top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why strings are immutable in python? [CLOSED]

+1 vote
249 views
Why strings are immutable in python? [CLOSED]
posted Dec 17, 2015 by Gnanendra Reddy

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

There is some advantage to have string as immutable.

First advantage is performance, knowing that a string is immutable means we can allocate space for it at creation time, and the storage requirements are fixed and unchanging. i.e. knowing that a string is immutable makes it easy to lay it out at construction time - fixed and unchanging storage requirements. This is also one of the reasons for the distinction between tuples and lists.

Another advantage is that strings in Python are considered as "elemental" as numbers. No amount of activity will change the value 8 to anything else, and in Python, no amount of activity will change the string "eight" to anything else.

Credit: http://tech.queryhome.com/67893/why-are-python-strings-immutable-what-does-befits-of-it

answer Dec 17, 2015 by Salil Agrawal
...