python字符串替换方法示例,python字符串示例,字符串替换可以用内置的方


字符串替换可以用内置的方法和正则表达式完成。

1.用字符串本身的replace方法:

a = 'hello word'b = a.replace('word','python')print b

2.用正则表达式sub来完成替换:

import rea = 'hello word'strinfo = re.compile('word')b = strinfo.sub('python',a)print b

评论关闭