Note that since this article was written (2004) CPython performs an in-place optimisation for assignments of the form<p><pre><code> s1 += s2
</code></pre>
There are details at point six of <a href="http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange" rel="nofollow">http://docs.python.org/2/library/stdtypes.html#sequence-type...</a>, where it also says that str.join() is preferable.
If you really care about efficiency (and are using PyPy), an actually efficient way to concat strings:<p><pre><code> from __pypy__.builders import StringBuilder
builder = StringBuilder(<size estimate, optional>)
for i in xrange(10000):
builder.append(str(i))
return builder.build()</code></pre>
Note these tests were run on Python 2.2 which makes the results old enough to almost certainly be invalid. Last time I checked, straight string concatenation was faster than everything else for the types of inputs I was dealing with.
How would a method 7 using generator expressions fare on the same system, like:<p><pre><code> return ''.join(`num` for num in xrange(loop_count))
</code></pre>
On one hand, it avoids creating a temporary list in memory. On the other, it can't know in advance how long the final output of the loop will be and so couldn't use tricks like preallocating enough RAM.
How have I never heard of backtick quotes? Apparently they are an abomination [1], so I will promptly force-forget them.<p>[1] <a href="http://stackoverflow.com/a/7490311/807674" rel="nofollow">http://stackoverflow.com/a/7490311/807674</a>
Doing stuff efficiently is great when there's some benefit from efficiency. But I've seen a lot of fragile code (and implementation specific hacks) created based on benchmarks, when the time savings were meaningless in practice.
How about using Python 3.3.2?<p>I did many kind of performance tests when implementing my PyClockPro caching lib, even if I don't use strings in it.