前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python字符串方法总结

Python字符串方法总结

作者头像
用户2700375
发布2022-06-09 14:08:33
3000
发布2022-06-09 14:08:33
举报
文章被收录于专栏:很菜的web狗

又是一篇菜鸡笔记 在学校很多毫无意义的事情要忙没那么多时间 自己学习效率也有待提高 今天正好有时间 加上课上也讲到了这些东西 还是比较难记的 所以总结一下 没有多少时间了 加油呀

str.strip()

清除字符串两端的空格 较常用

str.capitalize()

将首字符转换成大写 如果首字符没有大写形式 就返回原字符串

代码语言:javascript
复制
>>> 'xhy'.capitalize()
'Xhy'
>>>

str.title()

将字符串中每个首字母 大写。判断每个单词的依据则是基于空格和标点

代码语言:javascript
复制
>>> 'scholar xhy'.title()
'Scholar Xhy'
>>>

str.lower()

将字符串转换为小写 仅对ascii编码的字母有效

代码语言:javascript
复制
>>> 'XHY'.lower()
'xhy'
>>>

str.upper()

将字符串转换为大写 会自动忽略不可转换成大写的字符

代码语言:javascript
复制
>>> 'scholar10'.upper()
'SCHOLAR10'
>>>

str.center()

将字符串按照给定的宽度居中显示,可以给定特殊的字符 填充多余的长度 如果指定长度小于字符串长度 则返回原字符串

代码语言:javascript
复制
>>> '12345'.center(10,"*")
'**12345***'

str.count()

统计指定字符串 在原字符串中的个数

str.count()可以指定查询字符串的起止位置

代码语言:javascript
复制
>>> test='Scholars work very hard'
>>> test.count('a')
2
>>> test.count('a',5,8)
1
检验邮箱格式是否正确
代码语言:javascript
复制
email=input("请输入email邮箱")
if email.count("@")==1:
    
    if email.count(".")>=1:
        print("格式正确")
    else:
        print("邮箱应包含.")
else:
    print("邮箱中应包含@")

str.find() str.rfind()

从字符串左边或右边查找指定元素出现的索引位置 也可以指定 查找的起止范围 要查找的元素不存在时 返回-1

(不论左边还是右边查找得到的结果都是字符串左边数起的)

代码语言:javascript
复制
>>> text = 'puter protective covering'
>>> text.find('er')
3
>>> text.find('sc')
-1
>>> text.find('er',3)
3

str.index() str.rindex()

与 str.find() str.rfind()类似 不同的是如果要查找的元素不存在 则会引发ValueError

str.replice()

str.replice(old,new,count)

用新字符串代替旧字符串 可以指定替换次数

代码语言:javascript
复制
>>> text = 'qwe asd kkk lll qwe'
>>> text.replace('qwe','scholar')
'scholar asd kkk lll scholar'
>>> text.replace('qwe','scholar',1)
'scholar asd kkk lll qwe'

str.endswith() str.startswith()

判断字符串是否以某个指定的字符串 开头或结尾 返回布尔值 可指定查找的起止范围

代码语言:javascript
复制
>>> a="Scholars work very hard"
>>> a.endswith('rd')
True
>>> a.startswith("Sc")
True

str.split()

使用指定的字符将整个字符串拆分为若干个元素 并返回一个列表 ,默认没有参数时 拆分符为空格符

代码语言:javascript
复制
>>> '1,2,3'.split(',')
['1', '2', '3']
>>> 'i love python'.split()
['i', 'love', 'python']

str.isdigit()

isdigit() 方法检测字符串是否只由数字组成

代码语言:javascript
复制
>>> str = '123'
>>> str.isdigit()
True
>>> str = 'scholar'
>>> str.isdigit()
False

字符串方法的实际运用

任意读入一个字符串 判断是否为数字

代码语言:javascript
复制
s=input("请任意输入一个数字字符串")##-1.23 1.23
if s.startswith('-'):
    str1=s.split('-')##-1.23
    str2=str1[1]
elif s.startswith('+'):
    str1=s.split('+')
    str2=str1[1]
else:
    str2=s

dotCounts=str2.count('.')
if dotCounts==0 and str2.isdigit()==True:
    print("%s是一个数字"%(s))
elif dotCounts==1:
    strList=str2.split('.')
    if strList[0].isdigit() and strList[1].isdigit():
        print("%s是一个数字"%(s))
    else:
        print("%s不是一个数字"%(s))
else:
    print("%s不是一个数字"%(s))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • str.strip()
  • str.capitalize()
  • str.title()
  • str.lower()
  • str.upper()
  • str.center()
  • str.count()
    • 检验邮箱格式是否正确
    • str.find() str.rfind()
    • str.index() str.rindex()
    • str.replice()
    • str.endswith() str.startswith()
    • str.split()
    • str.isdigit()
    • 字符串方法的实际运用
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档