How do I replace a character in a string with another character in Python?
python, replace, string
Solution
Strings in Python are immutable, so you cannot change them in place. Check out the documentation of str.replace:
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
So to make it work, do this:
def changeWord(word):
for letter in word:
if letter != "i":
word = word.replace(letter,"!")
return word
Problem
I want to replace every character that isn't "i" in the string "aeiou" with a "!" I wrote: ``` def changeWord(word): for letter in word: if letter != "i": word.replace(letter,"!") return word ``` This just returns the original. How can I return "!!i!!"?