python中可以是使用re模块的sub或者subn模块来完成字符串的替换操作;
语法格式:
re.sub("replacement", "string", count = ?) ####count指定最替换几处,可以省略
re.subn("replacement", "string", count = ?) #####count指定最替换几处,可以省略,n意思是返回值会显示替换了几处
p = re.compile("(red|blue)") print(p.sub("color","blue and red cat")) print(p.subn("color","red and blue at and blue duck"))