Speed of string += string

Paul Moore gustav at morpheus.demon.co.uk
Sat Apr 12 20:06:50 CEST 2003

Mads Orbesen Troest <mads at troest.NEVERMORE.dk> writes:

> Given that strings are immutable, is it exceedingly slow (and memory 
> spiking) to do a lot of "string += string" operations in one's code? IOW, 
> does each and every += evaluation lead to a new concatenated copy of the 
> previous string (now freed for garbage collection) and the new string, so 
> that the longer the string to which stuff is appended is, the longer times 
> each += operation takes?

I don't know the underlying details too well (and I'm not sure they
are all that important) but yes, string += string does construct a new
string each time, and hence is relatively expensive.

The normal idiom for building a string in chunks is to create a list
of the chunks, and then join them once at the end. So you do:

    chunks = []
    while whatever():
    result = ''.join(chunks)

You could also use StringIO. However, all this does is basically what
I describe above, plus extra complexity to handle all the methods you
don't need. So you're better sticking with the above idiom.

This signature intentionally left blank

More information about the Python-list mailing list