#capitalize()把英文字符的第一个改为大写
>>> a = 'Hello World'
>>> a.capitalize()
'Hello world'
#把英文字符改为小写,与lower()一样
>>> a.casefold()
'hello world'
>>> a.lower()
'hello world'
#encode(encoding='utf-8',error='strict')以encoding指定的编码格式对字符串进行编码
>>> b = '你好!'
>>> b.encode('utf-8')
b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x81'
>>> b.encode('gbk')
b'\xc4\xe3\xba\xc3\xa3\xa1'
#endswith(sub[,start[,end]])检查字符串是否以sub字符串结尾,如果是返回True,否则返回False
>>> a = 'Hello World'
>>> a.endswith('l')
False
>>> a.endswith('d')
True
#expandtabs([tabsize = 8])把字符串中的tab符号(\t)转为空客,默认空格数是tabsize = 8,一个制表符(tab)等于8个空格(space)
>>> a = ' Hello World'
>>> a.expandtabs()
' Hello World'
>>> a.expandtabs(4)
' Hello Wolrd'
#lstrip()去掉字符串左边的所有空格
>>> a = ' Hello World'
>>> a.lstrip()
'Hello World'
#rstrip()去掉字符串右边的所有空格
>>> a = ' Hello World '
>>> a.rstrip()
'\tHello World'
#ljust(width)返回左对齐的字符串,并使用空格填充至长度为width的字符串
>>> a = ' Hello World'
>>> a.ljust(30)
' Hello World '
#rjust(width)返回右对齐的字符串,并使用空格填充至长度为width的字符串
>>> a = 'Hello World'
>>> a.rjust(30)
' Hello World'
#zfill(width)采用长度为wdith的字符串,原字符串右对齐,用0填充
>>> a = 'Hello World'
>>> a.zfill(30)
'0000000000000000000Hello World'
#partition(sub)找到子字符串sub,把字符串分成一个3元组(per_sub,sub,fol_sub),如果找不到sub子字符串则返回('原的字符串','','')
>>> a.partition('H')
('', 'H', 'ello World')
>>> a.partition('A')
('Hello World', '', '')
>>> a.partition('Hello')
('', 'Hello', ' World')
#rpartition(sub)找到子字符串sub,把字符串分成一个3元组(per_sub,sub,fol_sub),如果找不到sub子字符串则返回('','','原的字符串')
>>> a.rpartition('A')
('', '', 'Hello World')
>>> a.rpartition('H')
('', 'H', 'ello World')
>>> a.rpartition('Hello')
('', 'Hello', ' World')
#splitlines([keepends])按照'\n'分隔,返回列表数据类型,如果keepends参数指定,则返回keepends行
>>> a = '''Hello
World'''
>>> a
'Hello\nWorld'
>>> a.splitlines()
['Hello', 'World']
>>> a.splitlines(0)
['Hello', 'World']
>>> a.splitlines(1)
['Hello\n', 'World']
#swapcase()字符串中的大小写进行转换
>>> a = 'Hello World'
>>> a.swapcase()
'hELLO wORLD
#title()所有英文字符以大写开头,其余英文字符均小写
>>> a = 'hello world'
>>> a.title()
'Hello World'
#translate(table)采用table规则(由maketrans('old','new')定制)替换字符串中的字符,类似replace()
>>> b = a.maketrans('h','b')
>>> b
{104: 98}
>>> a.translate(b)
'bello world'
>>> a.replace('h','b')
'bello world'