logo头像
Snippet 博客主题

Python-字符串操作

字符串相关操作


连续空格去重

1
2
3
4
5
6
import re

re.sub(r'\s+', ' ', 'a b c d')

结果:
a b c d

格式化

1
2
3
4
5
6
7
8
9
s = 'hello {}'.format('zhangsan')
print(s)

s = 'hello {0}, where is {1}, {0}?'.format('zhangsan', 'lisi')
print(s)

结果:
hello zhangsan
hello zhangsan, where is lisi, zhangsan?

评论系统未开启,无法评论!