Concatenating Strings

Steven D'Aprano steve+comp.lang.python at
Fri Apr 10 07:17:31 CEST 2015

On Fri, 10 Apr 2015 04:44 am, Andrew Farrell wrote:

> I am under the impression that using format() is the canonically "right
> way to do it". It is certainly more readable than using ''.join() and is
> more semantically specific than string addition.

Certainly not. Using format in a loop to concatenate thousands of strings
will be horribly slow:

    text = ''
    for word in words:
        text = '{} {}'.format(text, word)

suffers from the problem that it has to create many intermediate strings
that are then just thrown away. The same applies to string interpolation
with the % operator:

    text = ''
    for word in words:
        text = '%s %s' % (text, word)

In both cases, use join:

    text = ' '.join(words)

which is much faster and easier to understand. Unlike repeated
concatenation, or repeated format/interpolation, join() can pre-allocate a
single block of memory for the new string, then fill it in, rather than go
through a loop of 

    allocate memory for a temporary string
    fill it in
    deallocate the previous temporary string

potentially thousands or millions of times.

But for the simple case where you are only concatenating a few substrings,
using format/interpolation is fine.


More information about the Python-list mailing list