image

'zhangxt'.center(7,'%')
'zhangxt'
a='zhangxt'
a.center(8,'#')
'zhangxt#'
a
'zhangxt'
a.center(9,'#')
'#zhangxt#'
a
'zhangxt'

image

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

image

'-='.join('python')
'p-=y-=t-=h-=o-=n'

image
image

a='PythOn'
a.swapcase()
'pYTHoN'

image

a='xtxtxtghghxt'
a.replace('xt','XT')
'XTXTXTghghXT'
a
'xtxtxtghghxt'
a.replace('xt','XT',2)
'XTXTxtghghxt'

image
拆分一个列表
默认以空格进行拆分

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']

image
默认删除空白

a=' zhangxt@'
a.strip('@')
' zhangxt'
a.strip()
'zhangxt@'
a
' zhangxt@'

image
image

a='@python@'
a.lstrip('@')
'python@'
a
'@python@'
a.rstrip('@')
'@python'
a
'@python@'
image
a.ljust(30,'@')
'zhangxt@@@@@@@@@@@@@@@@@@@@@@@'
image
a='python'
a.rjust(30,'@')
'@@@@@@@@@@@@@@@@@@@@@@@@python'

posted on 2025-09-07 12:55  青竹小轩  阅读(5)  评论(0)    收藏  举报