1.分割字符串
str.split(sep,maxsplit) #(分隔符,分几次)
2.合并字符串
str2=string.join(iterable) #str2='@'.join(list1) #list1是一个列表
3.检索字符串
- count()
- find()
- startwith()
- endwith()
4.字母大小写转换
- lower()
- upper()
5.去除字符串中的空格和特殊字符
- strip() #去除字符串左右的空格和特殊字符
- lstrip() #去除左侧的....
- rstrip() #去除右侧的...
6.格式化字符串
- 使用%
' %[-][+][0][m][.n]格式化字符 ' %a #- 可选参数,左对齐,正数前方无符号, #+ 可选参数,右对齐,正数前方加+号 #0 可选参数,由对齐,正数前方无符号,用0填充空白处,一般与m一起用 #m 可选参数,表示占有参数 #.n 可选参数,小数点后保留的位数 #格式化字符 用于指定类型 %s %d %f %e %%
- 使用Format()
print('结果为:{:.2f}'.format(a) ,'和', '{:.5f}'.format(b) ) print('结果为:{:.2f}'.format(a) +'和'+ '{:.5f}'.format(b) ) #都可以
7.编码和解码 python默认是采用UTF-8
str='黑~涛傻蛋' aa=str.encode() print(aa) bb=aa.decode() print(bb)
8.正则