python常用字符串函数
#字符串的操作
tstr = 'hello this world hello another world.txt'
print(tstr.find('ll')) #从左边开始寻找 #2
print(tstr.find('ww')) # -1 , 没找到
print(tstr.rfind('l')) #从右边 #34
print(tstr.rfind('.'))
print(tstr[tstr.rfind('.'):]) # .txt 找出格式信息
print(tstr.index('.')) # 36, 不同于find ,index未找到匹配字符会报错
print(tstr.count('w')) # 2, 打印出子字符串在 字符串出现的个数
tr = tstr.replace('hello', 'bye')
print(tr) # bye this world bye another world.txt
print(tstr.split(sep=' ', maxsplit=3)) # ['hello', 'this', 'world', 'hello another world.txt']
print(tstr.title()) #将每个首字母大写 Hello This World Hello Another World.Txt
print(tstr.capitalize()) #只把首个首字母大写 Hello this world hello another world.txt
print(tstr.startswith('th')) #检查是否以 该字符串 开头 False
print(tstr.endswith('txt')) #True
print(tstr.upper().lower()) #先将全部字母转换成大写,再全部为小写
tr1 = tstr.ljust(50)
print(tr1)
print(len(tr1)) # 50 , 返回与原字符串左对齐,右边增加空格, 长度为50的字符串
print(len(tstr)) #40
print(tstr.rjust(50)) #右对齐
print(tstr.center(50)) # hello this world hello another world.txt 居中
tr2 = tstr.rjust(50)
print(tr2) # hello this world hello another world.txt
print(tr2.lstrip()) #hello this world hello another world.txt #删除左边的空格
print(tstr.center(50).strip()) #删除两端的空格
print(tstr.partition('llo')) #以 'llo' 为界,进行分割 #('he', 'llo', ' this world hello another world.txt')
print(tstr.rpartition('llo')) # ('hello this world he', 'llo', ' another world.txt')
print(tstr.splitlines()) #依据行进行分割
print(tstr.isalpha()) #是否是字母
print(tstr.isalnum()) #所有是字母或数字
print(tstr.isdigit()) #是否只包含数字
print(tstr.isspace()) #空格
print(tstr)
print('tstr'.isidentifier()) #是否是字母开头
str3 = ','
str = ['helo', 'this', 'world']
print(str3.join(str)) # helo,this,world
tstr = tstr.split(sep=' ')
print(tstr) # ['hello', 'this', 'world', 'hello', 'another', 'world.txt']
print(str3.join(tstr)) # hello,this,world,hello,another,world.txt
项目推荐:
2000多G的计算机各行业电子资源分享(持续更新)
2020年微信小程序全栈项目之喵喵交友【附课件和源码】
Spring Boot开发小而美的个人博客【附课件和源码】
Java微服务实战296集大型视频-谷粒商城【附代码和课件】
Java开发微服务畅购商城实战【全357集大项目】-附代码和课件
最全最详细数据结构与算法视频-【附课件和源码】
还没有评论,来说两句吧...