exercise:
Write a function that receives a phrase and an old word and a new word The function should return a string containing the phrase original, but with the last occurrence of the old word replaced by the new word. The data input and output must be main program
I did as follows:
frase=input('digite uma frase:')
palavraAntiga = input('digite uma palavra da frase:')
palavraNova = input('digite uma palavra nova pra substitui-la:')
restante = frase.rsplit(palavraAntiga, 1)
frase = palavraNova.join(restante)
print(frase)
Somehow this way I did it right by doing the following test it does not go the way I want. Test:
digite uma frase:a a b b a c b d aa
digite uma palavra da frase:a
digite uma palavra nova pra substitui-la:KKK
a a b b a c b d aKKK
I would like to know how to make the word aa not replaced, but rather the word a so that it looks like this:
a a b b KKK c b d aa