[Tutor] string immutability

Steve Willoughby steve at alchemy.com
Mon Oct 24 20:34:51 CEST 2011

On Mon, Oct 24, 2011 at 01:04:20PM -0500, Johan Martinez wrote:
> Hi,
> I am struggling to understand Python string immutability. I am able to
> modify Python string object after initializing/assigning it a value. So how
> does immutability work? I am not following it. Sorry for really stupid
> question. Any help?

No, you're actualy not. 

> >>> s = "First"
> >>> print s
> First

At this point, you have created a string object with the value "First"
and put that into the variable "s" (which is glossing over a detail about 
how Python variables realy work, but that's another topic).

> >>> s = "Second"
> >>> print s
> Second

Now you created a new string object with the value "Second" and stored THAT
into s, replacing the original object, which is now lost.

Steve Willoughby    |  Using billion-dollar satellites
steve at alchemy.com   |  to hunt for Tupperware.

More information about the Tutor mailing list