I created this function:
def change(string,word1,word2):
string=string.split(" ")
for x in string:
if x==word1:
x=word2
string=" ".join(string)
print (string)
v1= "X went to buy food. X forgot his car keys in the car."
change(v1,"X","Y")
But the output is still:
"X went to buy food. X forgot his car keys in the car."
The output should be:
"Y went to buy food. Y forgot his car keys in the car."
What is wrong with the code? Why is it not working?
Edit: Guys, Thanks for all solutions, but I wonder Why is this not working? This is so that I don't do similar mistakes in future.