How is str.joiniterable method implemented in Python/ Linear time string concatenation
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
str.join is the standard Python tool for combining many strings efficiently. Its speed comes from avoiding the repeated allocation and copying that happen when you build a long result with + inside a loop.
Why Repeated + Can Be Expensive
Strings in Python are immutable. Every concatenation creates a new string, which means the characters already produced may need to be copied again and again.
With short inputs this is fine, but as the result grows, the total amount of copying can become quadratic in the total output size. That is why join is the idiomatic solution when you already have all the pieces.
The High-Level Strategy Used by str.join
At a high level, CPython handles sep.join(iterable) in two logical phases:
- Read the iterable and verify that every element is a string.
- Compute the total output size, allocate the destination once, then copy each piece and separator into that buffer.
That is what gives the operation linear behavior in the total number of characters copied.
The implementation includes a few fast paths:
- an empty iterable returns an empty string
- a single exact string item can often be returned directly
- the separator length is accounted for once during size calculation
The important idea is not the exact C function names. It is the allocation strategy: join knows the final size before it starts writing the output.
A Mental Model of the Internal Algorithm
The behavior is roughly equivalent to this conceptual implementation:
The real interpreter implementation is in C and avoids Python-level overhead, but the shape is similar: validate, size, allocate once, copy once.
One subtle detail is that join may need to materialize the iterable first. If you pass a generator, CPython still must inspect all items to know the final size. That means join is linear, but not streaming.
Compare join with Loop Concatenation
Here is a simple benchmark:
On typical CPython builds, join is clearly faster for large numbers of pieces. You may see optimizations for += in narrow cases, but they are implementation details and not something to rely on for predictable performance.
Type Constraints Matter
join requires strings. It does not silently convert arbitrary objects.
If conversion is intentional, do it explicitly:
This explicitness keeps the method fast and keeps accidental type bugs visible.
Common Pitfalls
- Building strings with
+inside a long loop when all pieces are already available. - Forgetting that
joinrequires strings and raisesTypeErrorfor other element types. - Assuming
joinstreams generator output directly. It still needs enough information to determine total size. - Overgeneralizing microbenchmarks. Small examples can hide the advantage that appears with larger inputs.
Summary
- '
str.joinis efficient because it computes the final size before writing the result.' - CPython avoids repeated reallocations by allocating the output buffer once.
- The operation is linear in the total number of characters copied.
- '
joinis the right tool when you already have many string fragments.' - Non-string items must be converted explicitly if that behavior is desired.
Related reading
- How is strong consistency possible given two generals problem
- How is the complexity of bucket sort is Onk if we implement buckets using linked lists?
- How is the complexity of PCA Ominp3,n3?
- How long is the SHA256 hash?
- How is tf.data.Dataset use optimised by tf.function in Tensorflow 2.0?
- How is the default max Java heap size determined?
- How is the R2 value in Scikit learn calculated?
- How many concurrent requests does a single Flask process receive?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.