In Python, a string is an immutable sequence. In this excerpt from Mark Lutz's Learning Python you'll learn how to change strings using concatenation and slicing. You'll also be introduced to Python formatting expressions.
Remember the term “immutable sequence”? The immutable part means that you can’t change a string in-place (e.g., by assigning to an index):
>>>S = 'spam'>>>S[0] = "x"Raises an error!
So, how do you modify text information in Python? To change a string, you need to build and assign a new string using tools such as concatenation and slicing, and then, if desired, assign the result back to the string’s original name:
>>>S = S + 'SPAM!'# To change a string, make a new one >>>S'spamSPAM!' >>>S = S[:4] + 'Burger' + S[−1]>>>S'spamBurger!'
The first example adds a substring at the end of S, by concatenation (really, it makes a
new string and assigns it back to S, but you can think of this as “changing”
the original string). The second example replaces four characters
with six by slicing, indexing, and concatenating. As you’ll see in
the next section, you can achieve similar effects with string method
calls like replace:
>>>S = 'splot'>>>S = S.replace('pl', 'pamal')>>>S'spamalot'
Like every operation that yields a new string value, string methods generate new string objects. If you want to retain those objects, you can assign them to variable names. Generating a new string object for each string change is not as inefficient as it may sound—remember, as discussed in the preceding chapter, Python automatically garbage collects (reclaims the space of) old unused string objects as you go, so newer objects reuse the space held by prior values. Python is usually more efficient than you might expect.
Finally, it’s also possible to build up new text values with string formatting expressions. Both of the following substitute objects into a string, in a sense converting the objects to strings and changing the original string according to a format specification:
>>>'That is %d %s bird!' % (1, 'dead')# Format expression That is 1 dead bird! >>>'That is {0} {1} bird!'.format(1, 'dead')# Format method in 2.6 and 3.0 'That is 1 dead bird!'
Despite the substitution metaphor, though, the result of formatting is a new string object, not a modified one. We’ll study formatting later in this chapter; as we’ll find, formatting turns out to be more general and useful than this example implies. Because the second of the preceding calls is provided as a method, though, let’s get a handle on string method calls before we explore formatting further.
Note
Python 3.0 and 2.6 introduce a new string type known as bytearray, which is
mutable and so may be changed in place. bytearray objects aren’t really strings;
they’re sequences of small, 8-bit integers. However, they support
most of the same operations as normal strings and print as ASCII
characters when displayed. As such, they provide another option
for large amounts of text that must be changed frequently. ord and chr handle Unicode characters, too,
which might not be stored in single bytes.
Google and YouTube use Python because it's highly adaptable, easy to maintain, and allows for rapid development. If you want to write high-quality, efficient code that's easily integrated with other languages and tools, this hands-on book will help you be productive with Python 3.0 quickly. Each chapter includes a unique Test Your Knowledge section with practical exercises and quizzes, so you can practice new skills and test your understanding as you go.




Help









