What is the most efficient way to concatenate many strings together?
PythonLevel: Intermediate
What is the most efficient way to concatenate many strings together?
Hints
- Identify object types, mutability and execution order.
- Trace a minimal example: which values and references change?
Answer
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
Source and licence: Python 3.14.0 Programming FAQ — Python Software Foundation · PSF-2.0