首页 > 应用开发 > IT综合 > python 字符串替换可以用2种方法实现:
下面用个例子来实验下:
a = 'hello word'
我把a字符串里的word替换为python
1用字符串本身的replace方法
a.replace('word','python')
输出的结果是hello python
2用正则表达式来完成替换:
import re
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b
输出的结果也是hello python
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29519108/viewspace-2127546/ ,如需转载,请注明出处,否则将追究法律责任。