python字符串替换

来源:互联网 发布:透明罗盘软件下载 编辑:程序博客网 时间:2024/05/23 16:48

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



原创粉丝点击