'zhangxt'.center(7,'%')
'zhangxt'
a='zhangxt'
a.center(8,'#')
'zhangxt#'
a
'zhangxt'
a.center(9,'#')
'#zhangxt#'
a
'zhangxt'
b='123454321'
b.count(4,3,6)
Traceback (most recent call last):
File "", line 1, in
TypeError: must be str, not int
b.count('4',3,6)
2
'-='.join('python')
'p-=y-=t-=h-=o-=n'
a='PythOn'
a.swapcase()
'pYTHoN'
a='xtxtxtghghxt'
a.replace('xt','XT')
'XTXTXTghghXT'
a
'xtxtxtghghxt'
a.replace('xt','XT',2)
'XTXTxtghghxt'
拆分一个列表
默认以空格进行拆分
a.split()
['blue', 'is', 'color']
a
'blue is color'
a.split('is')
['blue ', ' color']
b='x1y1z1'
b.split('1',1)
['x', 'y1z1']
b,split('1',3)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'split' is not defined
b.split('1',2)
['x', 'y', 'z1']
b='x1y1z1m'
b.split('1',3)
['x', 'y', 'z', 'm']
默认删除空白
a=' zhangxt@'
a.strip('@')
' zhangxt'
a.strip()
'zhangxt@'
a
' zhangxt@'
a='@python@'
a.lstrip('@')
'python@'
a
'@python@'
a.rstrip('@')
'@python'
a
'@python@'
a.ljust(30,'@')
'zhangxt@@@@@@@@@@@@@@@@@@@@@@@'
a='python'
a.rjust(30,'@')
'@@@@@@@@@@@@@@@@@@@@@@@@python'