Skip to main content
  1. About
  2. For Teams
Asked
Modified 6 months ago
Viewed 2.6m times
767

How do I efficiently append one string to another? Are there any faster alternatives to:

var1 = "foo"
var2 = "bar"
var3 = var1 + var2

For handling multiple strings in a list, see How to concatenate (join) items in a list to a single string.

See How do I put a variable’s value inside a string (interpolate it into the string)? if some inputs are not strings, but the result should still be a string.

1
  • 18
    TL;DR: If you're just looking for the simple way to append strings, and you don't care about efficiency: "foo" + "bar" + str(3)
    Andrew
    –  Andrew
    2018-06-13 15:29:31 +00:00
    Commented Jun 13, 2018 at 15:29

13 Answers 13

786

If you only have one reference to a string and you concatenate another string to the end, CPython now special cases this and tries to extend the string in place.

The end result is that the operation is amortized O(n).

e.g.

s = ""
for i in range(n):
    s += str(i)

used to be O(n^2), but now it is O(n).

More information

From the source (bytesobject.c):

void
PyBytes_ConcatAndDel(register PyObject **pv, register PyObject *w)
{
    PyBytes_Concat(pv, w);
    Py_XDECREF(w);
}


/* The following function breaks the notion that strings are immutable:
   it changes the size of a string.  We get away with this only if there
   is only one module referencing the object.  You can also think of it
   as creating a new string object and destroying the old one, only
   more efficiently.  In any case, don't use this if the string may
   already be known to some other part of the code...
   Note that if there's not enough memory to resize the string, the original
   string object at *pv is deallocated, *pv is set to NULL, an "out of
   memory" exception is set, and -1 is returned.  Else (on success) 0 is
   returned, and the value in *pv may or may not be the same as on input.
   As always, an extra byte is allocated for a trailing \0 byte (newsize
   does *not* include that), and a trailing \0 byte is stored.
*/

int
_PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
{
    register PyObject *v;
    register PyBytesObject *sv;
    v = *pv;
    if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) {
        *pv = 0;
        Py_DECREF(v);
        PyErr_BadInternalCall();
        return -1;
    }
    /* XXX UNREF/NEWREF interface should be more symmetrical */
    _Py_DEC_REFTOTAL;
    _Py_ForgetReference(v);
    *pv = (PyObject *)
        PyObject_REALLOC((char *)v, PyBytesObject_SIZE + newsize);
    if (*pv == NULL) {
        PyObject_Del(v);
        PyErr_NoMemory();
        return -1;
    }
    _Py_NewReference(*pv);
    sv = (PyBytesObject *) *pv;
    Py_SIZE(sv) = newsize;
    sv->ob_sval[newsize] = '\0';
    sv->ob_shash = -1;          /* invalidate cached hash value */
    return 0;
}

It's easy enough to verify empirically.

$ python -m timeit -s"s=''" "for i in xrange(10):s+='a'"
1000000 loops, best of 3: 1.85 usec per loop
$ python -m timeit -s"s=''" "for i in xrange(100):s+='a'"
10000 loops, best of 3: 16.8 usec per loop
$ python -m timeit -s"s=''" "for i in xrange(1000):s+='a'"
10000 loops, best of 3: 158 usec per loop
$ python -m timeit -s"s=''" "for i in xrange(10000):s+='a'"
1000 loops, best of 3: 1.71 msec per loop
$ python -m timeit -s"s=''" "for i in xrange(100000):s+='a'"
10 loops, best of 3: 14.6 msec per loop
$ python -m timeit -s"s=''" "for i in xrange(1000000):s+='a'"
10 loops, best of 3: 173 msec per loop

It's important however to note that this optimisation isn't part of the Python spec. It's only in the cPython implementation as far as I know. The same empirical testing on pypy or jython for example might show the older O(n**2) performance.

$ pypy -m timeit -s"s=''" "for i in xrange(10):s+='a'"
10000 loops, best of 3: 90.8 usec per loop
$ pypy -m timeit -s"s=''" "for i in xrange(100):s+='a'"
1000 loops, best of 3: 896 usec per loop
$ pypy -m timeit -s"s=''" "for i in xrange(1000):s+='a'"
100 loops, best of 3: 9.03 msec per loop
$ pypy -m timeit -s"s=''" "for i in xrange(10000):s+='a'"
10 loops, best of 3: 89.5 msec per loop

So far so good, but then,

$ pypy -m timeit -s"s=''" "for i in xrange(100000):s+='a'"
10 loops, best of 3: 12.8 sec per loop

ouch even worse than quadratic. So pypy is doing something that works well with short strings, but performs poorly for larger strings.

Sign up to request clarification or add additional context in comments.

12 Comments

Interesting. By "now", do you mean Python 3.x?
@Steve, No. It's at least in 2.6 maybe even 2.5
You've quoted the PyString_ConcatAndDel function but included the comment for _PyString_Resize. Also, the comment doesn't really establish your claim regarding the Big-O
congratulations on exploiting a CPython feature that will make the code crawl on other implementations. Bad advice.
Do NOT use this. Pep8 states explicitely: Code should be written in a way that does not disadvantage other implementations of Python (PyPy, Jython, IronPython, Cython, Psyco, and such, it then give this specific example as something to avoid since it's so fragile. Better use "".join(str_a, str_b)
|
373

Don't prematurely optimize. If you have no reason to believe there's a speed bottleneck caused by string concatenations then just stick with + and +=:

s  = 'foo'
s += 'bar'
s += 'baz'

That said, if you're aiming for something like Java's StringBuilder, the canonical Python idiom is to add items to a list and then use str.join to concatenate them all at the end:

l = []
l.append('foo')
l.append('bar')
l.append('baz')

s = ''.join(l)

4 Comments

I don't know what the speed implications of building your strings as lists and then .join()ing them are, but I find it's generally the cleanest way. I've also had great successes with using %s notation within a string for a SQL templating engine I wrote.
@Richo Using .join is more efficient. The reason is that Python strings are immutable, so repeatedly using s += more will allocate lots of successively larger strings. .join will generate the final string in one go from its constituent parts.
@Ben, there has been a significant improvement in this area - see my answer
Not directly related, but another advantage of this is that you easily add newlines between the elements of the list: '\n'.join(l)
66
str1 = "Hello"
str2 = "World"
newstr = " ".join((str1, str2))

That joins str1 and str2 with a space as separators. You can also do "".join(str1, str2, ...). str.join() takes an iterable, so you'd have to put the strings in a list or a tuple.

That's about as efficient as it gets for a builtin method.

2 Comments

What happens, if str1 is empy? Will the whitespace be set?
@JürgenK. Yes. It does not treat empty strings differently. It just takes all the strings and puts the sperator in between.
41

Don't.

That is, for most cases you are better off generating the whole string in one go rather than appending to an existing string.

For example, don't do: obj1.name + ":" + str(obj1.count)

Instead: use "%s:%d" % (obj1.name, obj1.count)

That will be easier to read and more efficient.

8 Comments

i'm sorry there is nothing more easier to read than ( string + string ) like the first example, the second example might be more efficient, but not more readable
@ExceptionSlayer, string + string is pretty easy to follow. But "<div class='" + className + "' id='" + generateUniqueId() + "'>" + message_text + "</div>", I find less readable and error-prone then "<div class='{classname}' id='{id}'>{message_text}</div>".format(classname=class_name, message_text=message_text, id=generateUniqueId())
Shadur-don't-feed-the-AI
This doesn't help at all when what I'm trying to do is the rough equivalent of, say, PHP/perl's "string .= verifydata()" or similar.
Shadur-don't-feed-the-AI
And in this case the answer to that question is "No, because that approach doesn't cover my use case"
With Python 3.6 we have f"<div class='{class_name}' id='{generateUniqueId()}'>{message_text}</div>"
|
34

Python 3.6 gives us f-strings, which are a delight:

var1 = "foo"
var2 = "bar"
var3 = f"{var1}{var2}"
print(var3)                       # prints foobar

You can do most anything inside the curly braces

print(f"1 + 1 == {1 + 1}")        # prints 1 + 1 == 2

Comments

19

If you're appending two strings then just use +.

If you need to do many append operations to build a large string, you can use StringIO. The interface is like a file: you write to append text to it, and then use getvalue() to get the combined string.

Comments

10

it really depends on your application. If you're looping through hundreds of words and want to append them all into a list, .join() is better. But if you're putting together a long sentence, you're better off using +=.

Comments

7

Basically, no difference. The only consistent trend is that Python seems to be getting slower with every version... :(


List

%%timeit
x = []
for i in range(100000000):  # xrange on Python 2.7
    x.append('a')
x = ''.join(x)

Python 2.7

1 loop, best of 3: 7.34 s per loop

Python 3.4

1 loop, best of 3: 7.99 s per loop

Python 3.5

1 loop, best of 3: 8.48 s per loop

Python 3.6

1 loop, best of 3: 9.93 s per loop


String

%%timeit
x = ''
for i in range(100000000):  # xrange on Python 2.7
    x += 'a'

Python 2.7:

1 loop, best of 3: 7.41 s per loop

Python 3.4

1 loop, best of 3: 9.08 s per loop

Python 3.5

1 loop, best of 3: 8.82 s per loop

Python 3.6

1 loop, best of 3: 9.24 s per loop

1 Comment

I guess it depends. I get 1.19 s and 992 ms respectively on Python2.7
6

Append strings with the add function:

str1 = "Hello"
str2 = " World"
str3 = str1.__add__(str2)
print(str3)

Output:

Hello World

3 Comments

str + str2 is still shorter.
@NikO'Lai, but not better. Using .__add__() you can concatenate text, in example, to output a dinamic text. You don't wan't to know variable names, or how many of them there are, you want just to add content. For me this seems excelent imho. Betther than storing the pieces of text into a list ...
@m3nda, to my knowledge, the operator + delegates to str.__add__(), so both should work equally. And you don't need to store text pieces in variables to be able to use +. This also works: greeting = "Hello" + " World" and even this works: greeting = "Hello" " World"
2
a='foo'
b='baaz'

a.__add__(b)

out: 'foobaaz'

2 Comments

Code is nice, but it would help to have an accompanying explanation. Why use this method rather than the other answers on this page?
Using a.__add__(b) is identical to writing a+b. When you concatenate strings using the + operator, Python will call the __add__ method on the string on the left side passing the right side string as a parameter.
0

One other option is to use .format as following:

print("{}{}".format(var1, var2))

Comments

0

Depends on what you are trying to do. If you are formatting a variable into a string to print, e.g. you want the output to be:

Hello, Bob

Given the name Bob, you'd want to us %s. print("Hello, %s" % my_variable) It's efficient, and it works with all data-types (so you don't have to do str(my_variable) like you do with "a" + str(5)).

Comments

0

You can use this to join strings: f"{var1} {var2}"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.