Skip to main content

Python program to replace words in a sentence


Write a program that replaces words in a sentence. The input begins with word replacement pairs (original and replacement). The next line of input is the sentence where any word on the original list is replaced.

Ex: If the input is:

automobile car manufacturer maker children kids The automobile manufacturer recommends car seats for children if the automobile doesn't already have one.

the output is:

The car maker recommends car seats for kids if the car doesn't already have one. 

You can assume the original words are unique.


 w = input().split()
 x = input()
 for i in range(0, len(w), 2):
         if w[i] in x:
                 x = x.replace(w[i], w[i+1])
 print(x)

Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Comments