>>> a = "i am happy"
>>> a.capitalize() #第一个字母转大写
'I am happy'
>>> a = "I lIKE FUCKing"
>>> a.casefold() #全部转小写(unicode)
'i like fucking'
>>> a.lower() #upper与此类似
'i like fucking'
>>> a
'I lIKE FUCKing'
>>> a.center(20) #居中
' I lIKE FUCKing '
>>> a.center(20, '#') #空白字符以#填充
'###I lIKE FUCKing###'
>>> a = a.casefold()
>>> a
'i like fucking'
>>> a.count('i')
3
>>> a.count('i', 2) #计数,从位置2开始
2
>>> a.endswith('ing') #以xx结尾 startswith与此类似
True
>>> a = "aaa\tbbb\tccc"
>>> a
'aaa\tbbb\tccc'
>>> a.expandtabs(5) #设置tab长度
'aaa bbb ccc'
>>> a = "i love fish ic"
>>> a.find('i') #寻找子串 rfind与此类似
0
>>> a.find('i', 1)
8
>>> a.find('w')
-1
>>> "{},{}".format("Hello", "world") #格式化字符
'Hello,world'
>>> "{0},{1},{0}".format("Hello", "world")
'Hello,world,Hello'
>>> b = {'name':'gh', 'age':20}
>>> "My name is {name}, my age is {age}".format_map(b) #格式化字符(字典)
'My name is gh, my age is 20'
>>> '123'.isalnum() #仅包含字母和数字(空字符串返回假)
True
>>> '123b'.isalnum()
True
>>> '12.34'.isalnum()
False
>>> '12ab'.isalpha() #全是字母
False
>>> 'abc'.isalpha()
True
>>> '123'.isdigit() #全是数字
True
>>> '123.45'.isdigit()
False
>>> '123a'.isdigit()
False
>>> '-34'.isdigit()
False
>>> '四'.isdigit()
False
>>> '四'.isnumeric() #支持汉字
True
>>> 'abcDe%'.islower() #全是小写
False
>>> 'abcde%'.islower()
True
>>> ' '.isspace() #全是空白字符
True
>>> ''.isspace()
False
>>> '\t \t'.isspace()
True
>>> '\t \n \t'.isspace()
True
>>> 'ABC'.isupper()
True
>>> 'xyz'.join(['aa', 'bb', 'cc']) #连接字符串
'aaxyzbbxyzcc'
>>> a = 'iamgh'
>>> a.ljust(10, '#') #左对齐
'iamgh#####'
>>> a.rjust(10)
' iamgh'
>>> ' are '
' are '
>>> a = ' are '
>>> a.lstrip()
'are '
>>> a.rstrip()
' are'
>>> a.strip() #去首尾空格
'are'
>>> a = '####are####'
>>> a.strip('#')
'are'
>>> a = 'iamfishc'
>>> a.partition('fi') #分割字符串
('iam', 'fi', 'shc')
>>> a.partition('ww')
('iamfishc', '', '')
>>> a.replace("i", "#") #替换,可指定次数
'#amf#shc'
>>> a.replace("i", "#", 1)
'#amfishc'
>>> a = "avc ert cew"
>>> a.split() #用空白字符分割
['avc', 'ert', 'cew']
>>> a.split(' ', 1) #指定分割次数
['avc', 'ert cew']
>>> a = "gh v587"
>>> a.split()
['gh', 'v587']
>>> a = "I am gh\nI am handsome \r\nI am kind"
>>> a.splitlines() #按行分割
['I am gh', 'I am handsome ', 'I am kind']
>>> a.splitlines(True) #保留换行符
['I am gh\n', 'I am handsome \r\n', 'I am kind']
>>> 'abCDefG'.swapcase()
'ABcdEFg'
>>> 'you are good'.title() #每个单词首字母大写
'You Are Good'
>>> '123'.zfill(5) #补0
'00123'
>>> '12343'.zfill(3)
'12343'