Recipe 1.6. Combining Strings
Credit: Luther Blissett
Problem
You have several small strings that you need to combine into one
larger string.
Solution
To join a sequence of small strings into one large string, use the
string operator join. Say that
pieces is a list whose items are strings, and you
want one big string with all the items concatenated in order; then,
you should code:
largeString = ''.join(pieces)
To put together pieces stored in a few variables, the
string-formatting operator % can often be even
handier:
largeString = '%s%s something %s yet more' % (small1, small2, small3)
Discussion
In Python, the
+ operator concatenates strings and therefore
offers seemingly obvious solutions for putting small strings together
into a larger one. For example, when you have pieces stored in a few
variables, it seems quite natural to code something like:
largeString = small1 + small2 + ' something ' + small3 + ' yet more'
And similarly, when you have a sequence of small strings named
pieces, it seems quite natural to code
something like:
largeString = ''
for piece in pieces:
largeString += piece
Or, equivalently, but more fancifully and compactly:
import operator
largeString = reduce(operator.add, pieces, '')
However, it's very important to realize that none of
these seemingly obvious solution is goodthe approaches shown
in the "Solution" are
vastly superior.
In Python, string objects are immutable. Therefore, any operation on
a string, including string concatenation, produces a new string
object, rather than modifying an existing one. Concatenating
N strings thus involves building and then
immediately throwing away each of N-1
intermediate results. Performance is therefore vastly better for
operations that build no intermediate results, but rather produce the
desired end result at once.
Python's string-formatting operator
% is one such operation, particularly suitable
when you have a few pieces (e.g., each bound to a different variable)
that you want to put together, perhaps with some constant text in
addition. Performance is not a major issue for this specific kind of
task. However, the % operator also has other
potential advantages, when compared to an expression that uses
multiple + operations on strings. % is more readable, once you get
used to it. Also, you don't have to call
str on pieces that aren't already
strings (e.g., numbers), because the format specifier
%s does so implicitly. Another advantage is that
you can use format specifiers other than %s, so
that, for example, you can control how many significant digits the
string form of a floating-point number should display.
|
Python does not
have a specific type called sequence, but
sequence is still an often-used term in
Python. sequence, strictly speaking, means: a
container that can be iterated on, to get a finite number of items,
one at a time, and that also supports indexing,
slicing, and being passed to the built-in function
len (which gives the number of items in a
container). Python lists are the
"sequences" you'll
meet most often, but there are many others (strings, unicode objects,
tuples, array.arrays, etc.).
Often, one does not need indexing, slicing, and
lenthe ability to iterate, one item at a
time, suffices. In that case, one should speak of an
iterable (or, to focus on the finite number of
items issue, a bounded iterable). Iterables that
are not sequences include dictionaries (iteration gives the
keys of the dictionary, one at a time in
arbitrary order), file objects (iteration gives the
lines of the text file, one at a time), and many
more, including iterators and generators. Any iterable can be used in
a for loop statement and in many equivalent
contexts (the for clause of a list comprehension
or Python 2.4 generator expression, and also many built-ins such as
min, max,
zip, sum,
str.join, etc.).
At http://www.python.org/moin/PythonGlossary,
you can find a Python Glossary that can help
you with these and several other terms. However, while the editors of
this cookbook have tried to adhere to the word usage that the
glossary describes, you will still find many places where this book
says a sequence or an
iterable or even a list, where, by
strict terminology, one should always say a bounded
iterable. For example, at the start of this
recipe's Solution, we say "a
sequence of small strings" where, in fact, any
bounded iterable of strings suffices. The problem with using
"bou |
|