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. Show The end result is that the operation is amortized O(n). e.g.
used to be O(n^2), but now it is O(n). From the source (bytesobject.c):
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. ❮ Python Glossary String ConcatenationString concatenation means add strings together. Use the Example x = "Python is " Try it Yourself » ExampleMerge variable a = "Hello" Try it Yourself » ExampleTo add a space between them, add a a = "Hello" Try it Yourself » For numbers, the Example x = 5 Try it Yourself » If you try to combine a string and a number, Python will give you an error: Example x = 5 Try it Yourself » Related PagesPython Variables Tutorial Creating Variables Variable Names Assign Value to Multiple Variables Output Variables Global Variables ❮ Python Glossary Can we append string in Python?In Python, the string is an immutable object. You can use the '+' operator to append two strings to create a new string. There are various ways such as using join, format, string IO, and appending the strings with space.
How do you add a string to a string?Concatenating Strings. Using the "+" operator − Java Provides a concatenation operator using this, you can directly add two String literals.. Using the concat() method − The concat() method of the String class accepts a String value, adds it to the current String and returns the concatenated value.. How do you append a character to a string in Python?In python, to append a string in python we will use the ” + ” operator and it will append the variable to the existing string.
How do you append text in Python?Append data to a file as a new line in Python. Open the file in append mode ('a'). Write cursor points to the end of file.. Append '\n' at the end of the file using write() function.. Append the given line to the file using write() function.. Close the file.. How do you add 3 strings in Python?To concatenate strings, we use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.
|