Why is a String Immutable?

An immutable object is one that, once created, will not change in its lifetime. In Python, strings are made immutable so that programmers cannot alter the contents of the object (even by mistake). This avoids unnecessary bugs.

Some other immutable objects are integer, float, tuple, and bool.

When a string reference is reinitialized with another worth, it is making another article instead of overwriting the past worth.

Note: In Python, strings are made changeless so software engineers can’t adjust the substance of the item. This keeps away from superfluous bugs.

We can verify this by trying to update a part of the string which will led us to an error.

# Can not reassign 
t= "Tutorialspoint"
print type(t)
t[0] = "M"

When we run the above program, we get the following output −

t[0] = "M"
TypeError: 'str' object does not support item assignment