In this article, we will be exploring on Python String’s Immutable Nature and how they are actually stored in the memory.
Before we move further, we will suggest you to re-visit your knowledge of Python Strings by checking this awesome tutorial on Python Strings .
What is the concept of Immutability ?
An immutable object is an object whose content (data stored in that object) can not be changed. If we talk about Python specifically, we have Numbers, Strings and tuples as immutable.
Immutable objects are better than Mutable objects in terms of performance and security.
Strings are Immutable. Why
Reason is simple, we can not modify the content of String object. Okay, but how ?
Let’s see an example to prove our point –
s = "code with me"
s[1] = 'k' # modifying the second character of string to 'k'
print(s)
In the above code, we have defined a string object, and we are trying to modify the second character of that string.
After running the code, we got an error -> TypeError: ‘str’ object does not support item assignment.
This error is the result of String’s Immutable Nature, which will not allow us to modify the content of any string object.
Now, look at this code –
s = "code with me"
print(s.replace('o', 'k', 1)) # replacing the first occurrence of 'o' with 'k'
Python String’s replace() method is used to replace one character or a subtring with another character or substring.
string.replace(old, new, count)
old : The string to search for
new : The string to replace the old value with
count : A number specifying how many occurrences of the old value you want to replace.
O/P : ckde with me
So, why this worked ? Isn’t that violating Strings Immutability Nature ?
No, its not. To justify this, let’s try to print the memory id for both statements,
s = "code with me"
print("Id of Replaced String : ", id(s.replace('o', 'k', 1)))
print("Id of Original String : ", id(s))
O/P :
Id of Replaced String : 2221657042160
Id of Original String : 2221657028016
Now, memory id for both is different. Hence, these are two different objects.
replace() method creates the copy of original string and modifies it. Hence, you are not seeing that Type Error anymore.
Let’s take one more ambiguous example,
In this example, we are creating two strings, ‘s1’ and ‘s2’ as :
s1 = "sun"
s2 = "fun"
Now, let’s assign content of s1 to s2.
s2 = s1
print(s2) # prints 'sun'
So, are they equal now ? Yes, they are. Again, isn’t that violates the String’s Immutability Nature ?
By looking at the code, it seems that content of s2 is replaced by the content of s1 and hence s2 became mutable. This is wrong assumption.
When we write, s2 = s1, the name ‘s2’ will be adjusted to refer to the object that is referred by ‘s1’. But, the original value of s2′ (which is “fun”) is not altered.
Since, “fun” is not referenced, the garbage collector deletes that object from memory. You can read this article to get more clear picture on Python Garbage collection.

s1 = "sun"
s2 = "fun"
print("Memory Id of s1", id(s1))
print("Memory Id of s2", id(s2))
s2 = s1
print("Memory Id of s1", id(s1))
print("Memory Id of s2", id(s2))
O/P :
Memory Id of s1 1496817399216
Memory Id of s2 1496817399344
Memory Id of s1 1496817399216
Memory Id of s2 1496817399216
We can see that initially, id for both s1, s2 was different, but after modification, their id became equal, and hence, s2 started referencing the new object.
In our opinion, this must be the best thing you know now. Please share and help us to reach out the corners of world.
Thanks and cheers.
