python字符串删除第n个字符 python怎么替换很多特定字符串为其他的字符串?

python怎么替换很多特定字符串为其他的字符串?用链式替换,示例如下:str1 = "abcdef"str2 = str1.replace("a","1").replace("b","2")prin

python怎么替换很多特定字符串为其他的字符串?

用链式替换,示例如下:

str1 = "abcdef"str2 = str1.replace("a","1").replace("b","2")print(str2) #12cdef

2.用正则替换,示例如下:

import restr3 = "abcdef"str4= re.compile("(a|b)").sub("1",str1)print(str4)#11cdef

1 & 2结合应该能解决问题