str转换字符串
>>> s = str(31)
>>> s
'31'
replace替换
>>> s = 'a' * 5
>>> s
'aaaaa'
>>> s.replace('a', 'b')
'bbbbb'
>>> s
'aaaaa'
>>> s = s.replace('a', 'b', 5)
>>> s
'bbbbb'
capitalize第一个字符变为大写
>>> str = "yang"
>>> str.capitalize()
'Yang'
upper转换成大写
>>> str.upper()
'YANG'
lower转换成小写
>>> str.lower()
'yang'
startswith(s)判断是否由s开头
>>> str.startswith('ya')
True
>>> str.startswith('xx')
False
enswith(s)判断是否由s结尾
>>> str.endswith('g')
True
isnumeric()判断是否为数字
>>> str.isnumeric()
False
isalpha()判断是否为字母
>>> str.isalpha()
True
split() 分割
>>> str = 'www.cctv.com'
>>> str
'www.cctv.com'
>>> list = str.split('.')
>>> list
['www', 'cctv', 'com']
join()合成
>>> '.'.join(list)
'www.cctv.com'
format合成(重要)
>>> name = 'tom'
>>> score = 20
>>> hi = 'second'
>>> '姓名:{0},分数:{1}, 工作:{2}'.format(name,score,hi)
'姓名:tom,分数:20, 工作:second'
>>> '{}{}{}'.format(name, score, hi)
'tom20second'
>>> '{0}, {1}, {length}'.format(name,score, length = '15')
'tom, 20, 15'
>>> '{0:10} = {1:10}'.format('yang', 123456)
'yang = 123456'
>>> '{0:>10}={1:10}'.format('yang',123456) 左对齐 右对齐
' yang= 123456'
>>> '{:f},{:.2f},{:06.2f}'.format(3.1415926, 3.1415926, 31.14159)
'3.141593,3.14,031.14'
>>> '{:x},{:o},{:b}'.format(123,123,123)
'7b,173,1111011'