How do I replace a word with another word using a loop. For example, let’s say I have a function called “changeWords” and I want this function to change three words can’t, shouldn’t, don’t to cannot, should not, do not. So when the function is entered, ‘changeWords("I don't know how to do this")' should return back “I do not know how to do this".
To clarify:
changeWords(“I can't eat") -> “I can not eat"
changeWords(“I don't like swimming.”) -> “I do not like swimming.”
changeWords(“I shouldn't do that.”) -> “I should not do that.”
My attempt:
def stringChange(a):
a = ""
for line in stringChange("a"):
line = text.replace("a","can't","can not")
if not line: break
return line
1 Answer