Mutable and Immutable Strings in python

Strings don’t have any methods on them that allow a change to the value of the string. In other words: strings are immutable.

When you call str.replace a new string is built and returned.

>>> s1 = 'Rohit'
>>> s2 = s1.replace('R', 'M')
>>> 
>>> s1
'Rohit'
>>> s2
'Mohit'

As you can see s1 still has its original value.

Leave a Comment