Which is the preferred way to concatenate a string in Python?

765,792

Solution 1

The best way of appending a string to a string variable is to use + or +=. This is because it's readable and fast. They are also just as fast, which one you choose is a matter of taste, the latter one is the most common. Here are timings with the timeit module:

a = a + b:
0.11338996887207031
a += b:
0.11040496826171875

However, those who recommend having lists and appending to them and then joining those lists, do so because appending a string to a list is presumably very fast compared to extending a string. And this can be true, in some cases. Here, for example, is one million appends of a one-character string, first to a string, then to a list:

a += b:
0.10780501365661621
a.append(b):
0.1123361587524414

OK, turns out that even when the resulting string is a million characters long, appending was still faster.

Now let's try with appending a thousand character long string a hundred thousand times:

a += b:
0.41823482513427734
a.append(b):
0.010656118392944336

The end string, therefore, ends up being about 100MB long. That was pretty slow, appending to a list was much faster. That that timing doesn't include the final a.join(). So how long would that take?

a.join(a):
0.43739795684814453

Oups. Turns out even in this case, append/join is slower.

So where does this recommendation come from? Python 2?

a += b:
0.165287017822
a.append(b):
0.0132720470428
a.join(a):
0.114929914474

Well, append/join is marginally faster there if you are using extremely long strings (which you usually aren't, what would you have a string that's 100MB in memory?)

But the real clincher is Python 2.3. Where I won't even show you the timings, because it's so slow that it hasn't finished yet. These tests suddenly take minutes. Except for the append/join, which is just as fast as under later Pythons.

Yup. String concatenation was very slow in Python back in the stone age. But on 2.4 it isn't anymore (or at least Python 2.4.7), so the recommendation to use append/join became outdated in 2008, when Python 2.3 stopped being updated, and you should have stopped using it. :-)

(Update: Turns out when I did the testing more carefully that using + and += is faster for two strings on Python 2.3 as well. The recommendation to use ''.join() must be a misunderstanding)

However, this is CPython. Other implementations may have other concerns. And this is just yet another reason why premature optimization is the root of all evil. Don't use a technique that's supposed "faster" unless you first measure it.

Therefore the "best" version to do string concatenation is to use + or +=. And if that turns out to be slow for you, which is pretty unlikely, then do something else.

So why do I use a lot of append/join in my code? Because sometimes it's actually clearer. Especially when whatever you should concatenate together should be separated by spaces or commas or newlines.

Solution 2

If you are concatenating a lot of values, then neither. Appending a list is expensive. You can use StringIO for that. Especially if you are building it up over a lot of operations.

from cStringIO import StringIO
# python3:  from io import StringIO

buf = StringIO()

buf.write('foo')
buf.write('foo')
buf.write('foo')

buf.getvalue()
# 'foofoofoo'

If you already have a complete list returned to you from some other operation, then just use the ''.join(aList)

From the python FAQ: What is the most efficient way to concatenate many strings together?

str and bytes objects are immutable, therefore concatenating many strings together is inefficient as each concatenation creates a new object. In the general case, the total runtime cost is quadratic in the total string length.

To accumulate many str objects, the recommended idiom is to place them into a list and call str.join() at the end:

chunks = []
for s in my_strings:
    chunks.append(s)
result = ''.join(chunks)

(another reasonably efficient idiom is to use io.StringIO)

To accumulate many bytes objects, the recommended idiom is to extend a bytearray object using in-place concatenation (the += operator):

result = bytearray()
for b in my_bytes_objects:
    result += b

Edit: I was silly and had the results pasted backwards, making it look like appending to a list was faster than cStringIO. I have also added tests for bytearray/str concat, as well as a second round of tests using a larger list with larger strings. (python 2.7.3)

ipython test example for large lists of strings

try:
    from cStringIO import StringIO
except:
    from io import StringIO

source = ['foo']*1000

%%timeit buf = StringIO()
for i in source:
    buf.write(i)
final = buf.getvalue()
# 1000 loops, best of 3: 1.27 ms per loop

%%timeit out = []
for i in source:
    out.append(i)
final = ''.join(out)
# 1000 loops, best of 3: 9.89 ms per loop

%%timeit out = bytearray()
for i in source:
    out += i
# 10000 loops, best of 3: 98.5 µs per loop

%%timeit out = ""
for i in source:
    out += i
# 10000 loops, best of 3: 161 µs per loop

## Repeat the tests with a larger list, containing
## strings that are bigger than the small string caching 
## done by the Python
source = ['foo']*1000

# cStringIO
# 10 loops, best of 3: 19.2 ms per loop

# list append and join
# 100 loops, best of 3: 144 ms per loop

# bytearray() +=
# 100 loops, best of 3: 3.8 ms per loop

# str() +=
# 100 loops, best of 3: 5.11 ms per loop

Solution 3

In Python >= 3.6, the new f-string is an efficient way to concatenate a string.

>>> name = 'some_name'
>>> number = 123
>>>
>>> f'Name is {name} and the number is {number}.'
'Name is some_name and the number is 123.'

Solution 4

Using in place string concatenation by '+' is THE WORST method of concatenation in terms of stability and cross implementation as it does not support all values. PEP8 standard discourages this and encourages the use of format(), join() and append() for long term use.

As quoted from the linked "Programming Recommendations" section:

For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b. This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.

Solution 5

You can do in different ways.

str1 = "Hello"
str2 = "World"
str_list = ['Hello', 'World']
str_dict = {'str1': 'Hello', 'str2': 'World'}

# Concatenating With the + Operator
print(str1 + ' ' + str2)  # Hello World

# String Formatting with the % Operator
print("%s %s" % (str1, str2))  # Hello World

# String Formatting with the { } Operators with str.format()
print("{}{}".format(str1, str2))  # Hello World
print("{0}{1}".format(str1, str2))  # Hello World
print("{str1} {str2}".format(str1=str_dict['str1'], str2=str_dict['str2']))  # Hello World
print("{str1} {str2}".format(**str_dict))  # Hello World

# Going From a List to a String in Python With .join()
print(' '.join(str_list))  # Hello World

# Python f'strings --> 3.6 onwards
print(f"{str1} {str2}")  # Hello World

I created this little summary through following articles.

Share:
765,792
Max
Author by

Max

Updated on August 29, 2021

Comments

  • Max
    Max over 2 years

    Since Python's string can't be changed, I was wondering how to concatenate a string more efficiently?

    I can write like it:

    s += stringfromelsewhere
    

    or like this:

    s = []
    
    s.append(somestring)
        
    # later
        
    s = ''.join(s)
    

    While writing this question, I found a good article talking about the topic.

    http://www.skymind.com/~ocrow/python_string/

    But it's in Python 2.x., so the question would be did something change in Python 3?

  • lvc
    lvc over 11 years
    cStringIO doesn't exist in Py3. Use io.StringIO instead.
  • Wes
    Wes over 11 years
    As for why appending to a string repeatedly can be expensive: joelonsoftware.com/articles/fog0000000319.html
  • Mikko Ohtamaa
    Mikko Ohtamaa over 11 years
    If you have multiple strings (n > 10) "".join(list_of_strings) is still faster
  • Mikko Ohtamaa
    Mikko Ohtamaa over 11 years
  • Admin
    Admin over 11 years
    the reason why += is fast is, that there is a performance hack in cpython if the refcount is 1 - it falls apart on pretty much all other python implementations (with the exception of a rather special configured pypy build)
  • Lennart Regebro
    Lennart Regebro over 11 years
    @Ronny: Sure, but you still need to profile to see when the problem arises. My timings as you see above are rather large strings. When you recommend "Use list.append/join" you make people do this when they concatenate just a few short strings, and that's not faster.
  • Wes
    Wes over 11 years
    Why is this being upvoted so much? How is it better to use an algorithm that is only efficient on one specific implementation and has what essentially amounts to a fragile hack to fix a quadratic time algorithm? Also you completely misunderstand the point of "premature optimization is the root of all evil". That quotation is talking about SMALL optimizations. This is going from O(n^2) to O(n) that is NOT a small optimization.
  • Wes
    Wes over 11 years
    Here is the actual quotation: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified"
  • Lennart Regebro
    Lennart Regebro over 11 years
    @Wes: Most string concatenations are small optimizations. It's only a big optimization if you are doing a lot of them. How do you know if you are doing a lot of them? It takes a long time. In that case it's useful to know that string concatenations are slow. It is not useful to do "".join([a, b]) instead of a+b everytime you need to concatenate two strings. most cases of you needing to use "".join() is when you have a long list of strings, and in that case "".join() is the simplest way to do it anyway.
  • Wes
    Wes over 11 years
    Nobody is saying that a + b is slow. It's quadratic when you are doing a = a + b more than once. a + b + c is not slow, I repeat not slow since it only has to traverse each string once, whereas it has to re-traverse the previous strings many times with the a = a + b approach (assuming that is in a loop of some kind). Remember strings are immutable.
  • Lennart Regebro
    Lennart Regebro over 11 years
    @Wes: And there you have already gone from "Which is the best way to concatenate a string" to "Which is the best way to concatenate a list of strings in Python. Two different questions. This is the first one. For the second one, in which you have a list of strings and you want to concatenate them, the answer is ''.join(thelist), regardless of speed, because that's the simplest and clearest way of doing it. But that is, as mentioned a completely different question.
  • Lennart Regebro
    Lennart Regebro over 10 years
    @Wes: I made a talk where I spend around 5 minutes dissecting this. + and += is faster or as fast in all cases when joining two strings and all platforms, except possibly in some extreme cases I didn't find. youtu.be/50OIO9ONmks?t=18m09s Slides: slides.colliberty.com/DjangoConEU-2013/#/step-40
  • Lennart Regebro
    Lennart Regebro over 10 years
    As you see from my answer, this depends on how many strings you are concatenating. I've done some timings on this (see the talk I linked to in my comments on my answer) and generally unless it's more than ten, use +.
  • Wes
    Wes over 10 years
    @Lennart, I would not rely on benchmarks in this case because whether += is optimized to not run in quadratic time depends on your particular implementation of Python. Even if most popular implementations do it, it's technically not guaranteed.
  • Lennart Regebro
    Lennart Regebro over 10 years
    @Wes: I tried multiple implementations and versions. It makes no difference. a += b is as fast or faster than ''.join([a, b]), and ''.join(alonglist) is faster than looping over alonglist and concatenating one by one.
  • Wes
    Wes over 10 years
    It isn't a part of the Python language that += must be optimized like that.
  • Lennart Regebro
    Lennart Regebro over 10 years
    @Wes I repeat: No optimization needed.
  • Wes
    Wes over 10 years
    You aren't really addressing my point, which is that strings are immutable in the python language, and the fact that += is optimized to append strings in O(1) time is an implementation detail.
  • Lennart Regebro
    Lennart Regebro over 10 years
  • user877329
    user877329 almost 7 years
    See stackoverflow.com/questions/34008010/…. Following the answers given in this thread, it is best to not concatenate strings.
  • Rick
    Rick almost 7 years
    str_join = lambda *str_list: ''.join(s for s in str_list)
  • Quantum7
    Quantum7 over 6 years
    PEP8 mentions this (python.org/dev/peps/pep-0008/#programming-recommendations). The rational is that while CPython has special optimizations for string concatenation with +=, other implementations may not.
  • Admin
    Admin over 6 years
    Reference link would have been nice :)
  • Charles Duffy
    Charles Duffy over 3 years
    If f'{a}{b}' isn't more efficient than a += b or a + b, I don't see how this is meaningfully responsive to a question that asks specifically about performance. This feature is syntax sugar (good and useful sugar, to be sure!), not a performance optimization.
  • Magnus Lind Oxlund
    Magnus Lind Oxlund almost 3 years
    What a ridiculous situation. It's one of the first things people are taught how to do, and here we have the wizards in the ivory tower issuing a PEP discouraging it because it's fragile.
  • khuongduybui
    khuongduybui over 2 years
    wait what? when you said "appending a list [is expensive]", you meant "appending a string" right?
  • jdi
    jdi over 2 years
    @khuongduybui it probably should say "appending TO a list is expensive"