一、replace()函数
1用字符串本身的replace方法:
a = 'hello word'
b = a.replace('word','python')
print b
二、re.sub()
import re
a = 'hello word'
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b
本文介绍了两种在Python中进行字符串替换的方法:使用内置的replace()函数和正则表达式的re.sub()函数。通过具体示例展示了如何将一个字符串中的特定子串替换成另一个子串。
一、replace()函数
1用字符串本身的replace方法:
a = 'hello word'
b = a.replace('word','python')
print b
二、re.sub()
import re
a = 'hello word'
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b
782

被折叠的 条评论
为什么被折叠?