可以使用字典来一次替换多个不同的字符串,例如:
text = "Hello World! This is a test."replace_dict = {"Hello": "Hi", "World": "Earth", "test": "example"}new_text = textfor old_str, new_str in replace_dict.itEMS(): new_text = new_text.replace(old_str, new_str)print(new_text)这段代码中,首先定义了一个包含要替换的字符串和对应替换字符串的字典replace_dict,然后使用for循环遍历字典中的键值对,对原始文本进行替换操作,最后打印出替换后的文本。




