-1

I've always thought that strings and tuples are immutable in Python. But now I've been confused with this function that I wrote (down below). When it's executed, I get a mutable string indeed. Am I missing something here?

def example():
    string = "/*Jon is @developer & musician"
    for char in string:
        if not char.isalpha():
            string = string.replace(char, '#')
    return(string)

Output:

>>> example()
'##Jon#is##developer###musician'
>>>

Thanks in advance for your explanation!

Mimo
  • 55
  • 4

1 Answers1

0

The string's replace method is not a in-place function. It creates a new string.

If instead you tried to run string[0] = "a" for example, you would have gotten an error.