python字符串一次替换多个字符使用replace替换多处

时间:2024-10-25 15:04:25

方法一:

适用替换少数量字符

str1 = "abc+1234"
str2 = str1.replace("a","").replace("b","").replace("c","")
# 得到 str2 = "+1234"

方法二:

适用替换多数量字符

str1 = "abcdefijklmn+1234"
for i in "abcdefijklmn":
	str1 = str1.replace(i,"")
# 得到 str1 = "+1234"

str2 = "r7&adhhfgerr,etrt3yge,ewfttwe"
for i in ['gerr',',etrt3yge',',ew']:
	str2 = str2.replace(i,"")
# 得到 str2 = "r7&adhhffttwe"

如果对你有帮助点赞支持+关注获取更多python知识!