1.name.__contains__ 包含
name1=‘eric‘
result = name.__contains__(er) #name是否包含er,包含返回true,不包含返回fals
print(result) #返回true
2.capitalize() 首字母大写
name = ‘liu‘
res = name.capitalize()
print (res)
3.center(width)
name = ‘liu‘
res = name.center(80,"*") #以80的宽度居中,以*填充
print (res)
4.count()
name = ‘liuliuliullllluuuuggg‘
res = name.count(‘l‘)#统计l出现的次数 res = name.count(‘l‘0,10)从0-10中字符串查找。
print (res)
5.endswith 判断是不是以什么字符结尾
name = ‘liu‘
res = name.endswith(‘u‘,0,3) #判断liu是否以u结尾,是返回true,不是返回false.
print (res)
6.find()
name = ‘liuliuliullllluuuuggg‘
res = name.find(‘l‘,0,n)#查找l所在的字符串位置,没有返回-1
res = name.index(‘l‘,0,n)#没有找到报错
print (res)
7.format
name = ‘liuguisheng{0} {1}‘res = name.format(‘handsome\t‘,‘age:28‘)print (res)返回:liuguishenghandsom age:28——————————————————————————————————name = ‘liuguisheng {name} {id}‘
res = name.format(name=‘liuguisheng‘,id=‘28‘)print (res)***********************************************8..join 字符串拼接li=[‘l‘,‘g‘,‘s‘]res = "".join(li)print(res)返回lgs————————————————————————————————————————————————9.ljust rjust(右对齐)name = ‘lgs‘res = name.ljust(10,"*")#左对齐————————————————————————————————————————————————10.partitionname = ‘liuguisheng‘res = name.partition(‘is‘)#将liuguisheng以is分割—————————————————————————————————————————————————11.replace
name = ‘liuguisheng‘res = name.replace(‘i‘,‘g‘,1)#将字符串的所有i找成g,后面加数字表示只转换第几个pint(res)lgugugsheng
时间: 2024-10-12 03:33:17