1
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 input and output of data should be done in the core program
I did it this way:
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)
In a way that I made it work but by doing the following test it does not come out 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 wonder how do I make that word aa not replaced but only the word to to keep it that way:
a a b b KKK c b d aa