#将首字母大写
s1 = "hello world"
s2 = str1.capitalize()
s2
'Hello world'
#去除大小写差异,统一变为小写
s1 = "AbcDR"
s1.casefold()
'abcdr'
#将某个字符串补长至多少位,源字符串位于中间
s1 = "abcBBfgh"
s2 = s1.center(20,'*')
s2
'******abcBBfgh******'
#返回某个子字符串的个数
s1 = "abcBBfgh"
s2 = s1.count('cB')
s2
1
#将字符串编码,默认 utf-8
s1 = "hello python"
s2 = s1.encode()
type(s2)
bytes
#判断某字符串是否以某后缀结尾
s1 = "hello python"
s2 = s1.endswith('n')
s2
True
#将 '\t' 用若干空格代替,默认是8
s1 = "hello python\t"
s2 = s1.expandtabs(4)
s2
'hello python '
#返回子字符串第一次出现的位置,如果没有,则返回-1
s1 = "hello world"
s2 = s1.find('wd')
s2
-1
#返回子字符串第一次出现的位置,如果没有,会报错
s1 = "hello world"
s2 = s1.index('wo')
s2
6
#判断某字符串是否为字母或数字组成的字符串
s1 = "5656"
s2 = s1.isalnum()
s2
True
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)