Run
# Define a dictionary to map substrings to their replacements replacement_dict = { 'apple': 'fruit', 'carrot': 'vegetable', 'dog': 'animal' } # Your input string x = "I have an apple and a dog. The apple is red, and the dog is brown." # Replace substrings based on the dictionary for old_str, new_str in replacement_dict.items(): x = x.replace(old_str, new_str) # Print the modified string print(x)
Output