python 字符串替换可以用2种方法实现:

转自:http://blog.sina.com.cn/s/blog_71f389090100vft4.html

python 字符串替换可以用2种方法实现:
1是用字符串本身的方法。
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

至于用哪个方法的话,看你自己的选择了。
请使用浏览器的分享功能分享到微信等