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?
In Python >= 3.6, the new f-string is an efficient way to concatenate a string.
While somewhat dated, Code Like a Pythonista: Idiomatic Python recommends
join()
over+
in this section. As does PythonSpeedPerformanceTips in its section on string concatenation, with the following disclaimer: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.
You write this function
Then you can call simply wherever you want
If the strings you are concatenating are literals, use String literal concatenation
This is useful if you want to comment on part of a string (as above) or if you want to use raw strings or triple quotes for part of a literal but not all.
Since this happens at the syntax layer it uses zero concatenation operators.
As @jdi mentions Python documentation suggests to use
str.join
orio.StringIO
for string concatenation. And says that a developer should expect quadratic time from+=
in a loop, even though there's an optimisation since Python 2.4. As this answer says:I will show an example of real-world code that naively relied on
+=
this optimisation, but it didn't apply. The code below converts an iterable of short strings into bigger chunks to be used in a bulk API.This code can literary run for hours because of quadratic time complexity. Below are alternatives with suggested data structures:
And a micro-benchmark: