文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- Zoom.Quiet [2004-08-11 17:43:49]

1. 合并字符串

3.6 Combining Strings

合并字符串

Credit: Luther Blissett

1.1. 问题 Problem

You have several small strings that you need to combine into one larger string.

你有几个小的字符串,你想吧他们合并成一个大的字符串。

1.2. 解决 Solution

The + operator concatenates strings and therefore offers seemingly obvious solutions for putting small strings together into a larger one. For example, when you have all the pieces at once, in a few variables:

'+' 操作符连接字符串,它为把几个小字符串连在一起变成一个大字符串提供了看起来很直观的解决方案。例如, 当你有所有的字符串片断,保存在几个变量里时:

   1 largeString = small1 + small2 + ' something ' + small3 + ' yet more'

Or when you have a sequence of small string pieces:

或者,当你有小的字符串片断的一个序列:

   1 largeString = ''
   2 for piece in pieces:
   3     largeString += piece

Or, equivalently, but a bit more compactly: 或者,相等地,然而很简洁:

   1 import operator
   2 largeString = reduce(operator.add, pieces, '')

However, none of these solutions is generally optimal. To put together pieces stored in a few variables, the string-formatting operator % is often best:

然而,这些解决方案中没有一个是最佳的。为了把短的片断放在少数的变量中,字符串操作符'%'经常是更好的选择!

   1 largeString = '%s%s something %s yet more' % (small1, small2, small3)

To join a sequence of small strings into one large string, the string operator join is invariably best:

为了加入一系列的短字符串到一个大的字符串中,字符串操作符join总是最好的:

   1 largeString = ''.join(pieces)

1.3. 讨论 Discussion

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 quite a bit better for operations that build no intermediate results, but rather produce the desired end result at once. The string-formatting operator % is one such operation, particularly suitable when you have a few pieces (for example, each bound to a different variable) that you want to put together, perhaps with some constant text in addition. In addition to performance, which is never a major issue for this kind of task, the % operator has several potential advantages when compared to an expression that uses multiple + operations on strings, including readability, 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中,字符串对象是不变的。所以,在字符串上的任何操作,包括字符串连接,都产生一个 新的字符串对象,而不是更改一个存在的字符串。连接N个字符串因而包括构造并且然后立刻抛出 N-1个中间结果中的每一个。因此不构造中间结果,而直接产生最终期望结果的操作在性能上要稍好 一点.字符串格式操作符'%'就是这样一个操作。它很适合当你有少量字符串片断(例如,每一个都 被绑定在不同的变量上)你想把他们放在一起,或许另外还带有一些常量的字符串。除了增加性能 (对于这样的任务,性能从来不是主要问题),一旦你使用它,与在字符串上使用多个+操作表达式 相比,%操作符有好几种潜在的好处,包括可读性好。同样,你不用在每个不是字符串的片断上调 用str,因为格式化标识符%s隐式的那样做了。另一个好处是可以使用'%s'外的其它格式符,例如, 你可以控制用多少位有效数字来显示浮点数.

When you have many small string pieces in a sequence, performance can become a truly important issue. The time needed for a loop using + or += (or a fancier but equivalent approach using the built-in function reduce) tends to grow with the square of the number of characters you are accumulating, since the time to allocate and fill a large string is roughly proportional to the length of that string. Fortunately, Python offers an excellent alternative. The join method of a string object s takes as its only argument a sequence of strings and produces a string result obtained by joining all items in the sequence, with a copy of s separating each item from its neighbors. For example, ‘’.join(pieces) concatenates all the items of pieces in a single gulp, without interposing anything between them. It's the fastest, neatest, and most elegant and readable way to put a large string together.

当你在一个序列中有许多小的字符串片断,性能成为一个真正重要的问题。对于一个循环使用+或 者+=(或者一个使用内建函数reduce同等方法的爱好者)所需要的时间趋向于你正在聚集的字符的数 量增长的平方。因为分配的时间和填充一个大的字符串大略是和字符串的长度成比例的。幸运的是 ,python提供了一个优秀的可选方法。一个字符串对象s的join方法把一个字符串序列作为唯一的参数, 它把序列中所有项合起来生成一个字符串,并且用s把各项与相邻的项分隔开。例如,‘’.join(pieces) 在一个单独的gulp里连接所有项,在他们之间没有任何分隔符.这是一个把大量字符串合并在一起的最快, 最整洁,最优雅的方法,并且是有很好的可读性.

Even when your pieces come in sequentially from input or computation, and are not already available as a sequence, you should use a list to hold the pieces. You can prepare that list with a list comprehension or by calling the append or extend methods. At the end, when the list of pieces is complete, you can build the string you want, typically with ‘’.join(pieces). Of all the handy tips and tricks I could give you about Python strings, I would call this one the most significant.

甚至当你的片断来自于一系列用户输入或者计算,并且不能作为一个序列来利用的时候,你可以使用一 个list去容纳每个片断。你能通过list内涵或者调用append或extend方法准备那样的list。在最后 ,当一个片断的list完成时,你能用典型的‘’.join(pieces)构建你想要的字符串。 我能够给你的关于字符串的技巧和窍门中,我愿意把这个看作是最重要的。

1.4. 参考 See Also

The Library Reference sections on string methods, string-formatting operations, and the operator module.