Understanding String Immutability


Sequences fall into one of two categories: mutable or immutable. (Again, more fancy computer jargon.) Mutable means changeable. So, a sequence that's a mutable sequence is one that can change. Immutable means unchangeable. So, a sequence that's immutable is one that can't change. Strings are immutable sequences, which means that they can't change. So, for example, the string "Game Over!" will always be the string "Game Over!". You can't change it. In fact, you can't change any string you create. Now, you might think, from your experience with strings, that I'm totally wrong on this. You might even run an interactive session to prove that you can change a string, maybe something resembling this:

 >>> name = "Chris" >>> print name Chris >>> name = "Jackson" >>> print name Jackson 

You might offer this as proof that you can change a string. After all, you changed the string "Chris" to "Jackson". But, you didn't change any strings in this session. You just created two different strings. First, you created a string "Chris" and assigned it to the variable name. Then, you created another string, "Jackson", and assigned it to name. Now, both "Chris" and "Jackson" are great names, but they're different names and always will be, just as they are different strings and always will be. Take a look at Figure 4.7 for a visual representation of what happened in the interactive session.

click to expand
Figure 4.7: First, name gets the string "Chris", then it gets a different string, "Jackson". But no string values ever change.

Another way to think about this is to imagine that strings are written in ink on pieces of paper. You can throw out a piece of paper with a string on it and replace it with another piece of paper with a new string on it, but you can't change the words once they've been written.

You might think this is much ado about nothing. So what if a string is immutable? But string immutability does have consequences. Since you can't change a string, you can't assign a new character to a string through indexing. Here's an interactive session to show you what I mean:

 >>> word = "game" >>> word[0] = "l" Traceback (most recent call last):   File "<pyshell#1>", line 1, in ?     word[0] = "l" TypeError: object doesn't support item assignment 

In this session, I wanted to change the string "game" to the string "lame" (obviously, I didn't much like the game I was referring to). All I needed to do was change the letter "g" to an "l." So I just assigned "l" to the first position in the string, word[0]. But as you can see, this resulted in a big, fat error. The interpreter even tells me that strings don't support item assignment (you can't assign a new value to a character in a string).

But, just because you can't alter a string doesn't mean you can't create new strings from existing ones.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net