0

I want my code to find and replace a word, but I don't want it to modify my text

I would like my code to do something like that :

text = 'Hello my pseudo is LittlePig and I am Happy'
word = 'littlepig'
newWord = 'bigTurtle'

if word in text:
     text.replace(word, newWord)

print(text)

text -> Hello my pseudo is bigTurtle and I am happy

I will explain myself briefly, I want my code to find if a word is in a string to replace it by another. But the problem is that it won't work if the word is not written exactly with the same uppercase and lowercase letters like the one in the text.

So I found a solution, it was to put all the text in lower and the word too, but at the final the text string would be entirely in lower and I don't want that too.

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
Canapy
  • 223
  • 1
  • 2
  • 4

0 Answers0