字符串替换
2017-11-14 本文已影响0人
blank824
python字符串替换有两种方法可以实现
1.字符串本身的方法
2.正则表达式
a='hello world'
1.用字符串本身的替换方法
b=a.replace('world', 'test')
输出hello test
2.正则表达式替换
import re
b=re.compile('world').sub('test',a)
python字符串替换有两种方法可以实现
1.字符串本身的方法
2.正则表达式
a='hello world'
1.用字符串本身的替换方法
b=a.replace('world', 'test')
输出hello test
2.正则表达式替换
import re
b=re.compile('world').sub('test',a)