I was just going through mutable and immutable structures in python. It was written that "Strings are immutable" in Python i.e We cannot alter them Consider the code:
str1='Rohit'
str1.replace('R','M')
This gives the output:
'Mohit'
Now, somebody said that it is the variable str1 which is pointing to the string 'Rohit' and after str1.replace() it points to 'Mohit' Consider this code:
'Rohit'.replace('R','M')
This also gives me the output:
'Mohit'
Then what is meant by 'Strings are immutable'?
6 Answers 6
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.
-
3>>>a='Rohit' >>> id(a) 52641584L >>> a=a.replace('R','M') >>> a 'Mohit' >>> id(a) 38710648L This cleared the confusionuser8914517– user891451711/14/2017 17:15:13Commented Nov 14, 2017 at 17:15
Strings are known as Immutable in Python (and other languages) because once the initial string is created, none of the function/methods that act on it change it directly, they simply return new strings.
So, in your example
str1='Rohit'
str1.replace('R','M')
After the .replace
call, you should try evaluating str1
in your REPL. It might surprise you to see that str1
still holds the value 'Rohit'. The .replace
returns a new string in which all instances of the given character are replaced with the specified character. Your REPL will print that returned string, but after that it is lost since you didn't assign it to anything.
Your second example is much the same, but with a string constant instead of a variable like str1
.
you can check mutability by checking id of variable before and after the change
In [3]: s1 = 'Rohit'
In [4]: id(s1)
Out[4]: 140510191375440
In [5]: s1.replace('R', 'M')
Out[5]: 'Mohit'
In [6]: id(s1)
Out[6]: 140510191375440
-
4That does not really check whether the string
s1
is pointing to was changed, it merely checks whether it still points to the same instance.tobias_k– tobias_k11/14/2017 14:08:16Commented Nov 14, 2017 at 14:08 -
1This is wrong. Simple counter-example: a dict is mutable, but the id stays the same, even after changing it.Peter– Peter11/29/2023 09:04:18Commented Nov 29, 2023 at 9:04
Yes Python strings are immutable for sure. Lets suppose you have
s = "Rohit"
now s correctly points to the string object "Rohit". When you do something like
s.replace("R", "M")
this will not change s in place. The .replace method will replace "R" with "M" and then return the newly created object. In your case, it will be Mohit. Thats the reason you are able to see "Mohit" as a return value but its not changing s. Its creating a new string and returning that.
You are getting such output because that line of code does nothing to the string but creates a new string with R replaced with M (you can think it returned a variable), and in python when you simply type the name of a variale it is printed on the console. That's what created the confusion, I think.
You can pass strings to functions which return a new string (as replace
).
Strings don't have methods operating on themselves
; we can try this, though:
>>> s1 = "abcd"
>>> s1[1:3]
'bc'
>>> s1[2]
'c'
>>> s1[2] = 'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
We can use subscripts on a string as if it were a list, but they are not writeable.
str1
will not change unless you dostr1 = str1.replace(...)
, in which case it is assigned a new string, and the old one is discarded, but not changed.+=
(ie. append) works is even more perplexing WRT "immutability".