I have the following sentence "the man went to the shop" and I want to replace any word that has "th" or "sh" with "dogs. The results should look something like this:
dogse man went to dogse dogsop
This is what my code looks like so far:
sentence = "the man went to the shop"
to_be_replaced = ["th", "sh"]
replaced_with = "dogs"
for terms in to_be_replaced:
if terms in sentence:
new_sentence = sentence.replace(terms,replaced_with)
print new_sentence
Currently, this prints:
dogse man went to dogse shop
the man went to the dogsop
I want it to print this only:
dogse man went to dogse dogsop
I would I go about doing this?