String concatenation vs. string formatting

Ian Kelly ian.g.kelly at gmail.com
Fri Jul 8 23:38:18 CEST 2011


On Fri, Jul 8, 2011 at 3:23 PM, Benjamin Kaplan
<benjamin.kaplan at case.edu> wrote:
> String formatting is the One Right Way here. It's fine to use string
> concatenation for a few things, but the operation is O(n^2) because each
> concat occurs one at a time: Python allocates space for a string the size of
> the first 2 things, copies the contents over. Then allocate a string the
> size of that string plus the third string and copy the contents over. It can
> get pretty slow if you're building a really big string With string
> formatting, Python creates a single string large enough to copy all the
> formatting arguements in and then copies the contents over once.

This argument doesn't really fly, because a string formatting
operation is typically used as an alternative for a constant number of
concatenations, not O(n) concatenations.  As such, either approach
would be O(n) for a single instance.

In the case that you do need to do O(n) concatenations, it is usually
best to use a StringIO object, or to build a list and then call
str.join.

> Also, string formatting (especially using the new syntax like you are) is
> much clearer because there's less noise (the quotes all over the place and
> the plusses) and it's better for dealing with internationalization if you
> need to do that.

This is the real reason to prefer string formatting over
concatenation.  It's also much less clutter to be able to use the %s
placeholder rather than having to call str() on everything.



More information about the Python-list mailing list