Python - Replacing letters in a string?

python

Solution

`strings` are immutable, you need to assign it to a new variable and return that. `replace()` returns a new string and does not change it in place.

>>> def letter_replace(strng, letter, replace):
    replace = str(replace)
    for char in strng:
        if char == letter.upper() or char == letter.lower():
            strng = strng.replace(char, replace)
            return strng   # Or just do return strng.replace(char, replace)
        else:
            return "Sorry, the letter could not be replaced."


>>> letter_replace('abc', 'a', 'f')
'fbc'

Problem

I am basically writing a simple function in which the user enters a sentence (strng), a letter (letter) and another letter (replace) to replace the first letter with. Here's what I have: ``` def letter_replace(strng, letter, replace): replace = str(replace) for char in strng: if char == letter.upper() or char == letter.lower(): strng.replace(char, replace) return strng else: return "Sorry, the letter could not be replaced." ``` I can't figure out why this won't work. Sorry if it's a completely obvious mistake, I am fairly new to Python. Thanks

Original source