Do Not Use “+” to Join Strings in Python
There’s a better, faster, and cleaner way to handle string concatenation — and you’re probably ignoring it.

Don’t Let This Slow You Down
Do Not Use “+” to Join Strings in Python
When you’re writing Python code, string manipulation is inevitable. Whether you’re formatting logs, generating dynamic SQL queries, or constructing HTML, you will be joining strings — and chances are, you’re doing it wrong.
If you’ve ever written something like this:
full_name = first_name + " " + last_name
…you’re not alone. It’s quick, easy, and it works.
But what if I told you that using the +
operator to join strings is a silent performance killer, a readability nightmare in disguise, and a practice you should stop immediately — especially in production-level code?
Let’s talk about why you should rethink your string concatenation habits.
The Illusion of Simplicity
At first glance, the +
operator feels natural for string joining. It mimics arithmetic addition, it's readable, and it’s everywhere in beginner tutorials.
greeting = "Hello, " + name + "! Welcome."
Seems harmless, right?
But under the hood, Python is doing more work than you might think. Strings in Python are immutable, which means every +
operation creates a new string in memory. Multiply this across loops or large concatenations, and you’re not just writing inefficient code — you’re hurting performance.
Why String “+” Is a Problem
Here’s a real example that might seem innocent:
items = ['apple', 'banana', 'cherry']
result = ''
for item in items:
result += item + ', '
This looks fine but is actually terrible for performance. Each iteration creates a brand-new string. Python has to copy the old string, append the new part, and assign the result back — every single time.
Imagine this with thousands of items. The memory overhead and CPU time add up fast.
Benchmark: +
vs .join()
Let’s compare using +
and str.join()
:
import time
# Using +
start = time.time()
result = ''
for i in range(10000):
result += str(i)
end = time.time()
print("Using +:", end - start)
# Using join
start = time.time()
result = ''.join(str(i) for i in range(10000))
end = time.time()
print("Using join:", end - start)
Output:
Using +: 0.47 seconds
Using join: 0.02 seconds
Yes, .join()
is more than 20 times faster in this simple case.
The Right Way: Use str.join()
Python provides a built-in method that’s optimized for concatenating strings: str.join()
.
items = ['apple', 'banana', 'cherry']
result = ', '.join(items)
print(result)
Output:
apple, banana, cherry
Why join()
Wins:
- Performance: Optimized for joining multiple strings at once.
- Memory-efficient: Does not repeatedly create new string objects.
- Clean syntax: Especially for lists or generators.
- Scalable: Works great for large datasets or loops.
When Is +
Acceptable?
Now, to be fair, using +
for short, one-off strings isn’t always a sin.
full_name = first_name + " " + last_name
That’s perfectly readable and not a big deal for performance. But once you move to loops, repeated operations, or joining many strings — switch to join()
without hesitation.
What About f-strings?
Great question.
Python 3.6 introduced f-strings, which are both fast and readable.
name = "Alice"
greeting = f"Hello, {name}!"
F-strings internally optimize the string formatting process and are generally preferred over +
.
If you’re not joining a list of strings but rather inserting variables, f-strings are your best bet. They’re faster than +
and more concise than format()
.
Real-World Use Case: Logging
Let’s say you’re building logs in a web server loop:
log_message = "[INFO] User: " + user_id + " accessed endpoint: " + endpoint
That’s painful to read and inefficient at scale.
Instead:
log_message = f"[INFO] User: {user_id} accessed endpoint: {endpoint}"
Or, if you’re collecting dynamic parts:
log_parts = [timestamp, user_id, endpoint, status]
log_message = ' | '.join(log_parts)
This is cleaner, faster, and easier to maintain.
Summary: Best Practices
Here’s a quick cheat sheet:
Use str.join()
when:
- You’re concatenating elements of a list or generator.
- Performance matters.
- The number of strings is dynamic or large.
Use f-strings when:
- You’re formatting strings with variables.
- Readability and clarity are important.
Avoid using +
when:
- Joining strings in loops.
- Working with large datasets.
- Writing production code or libraries.
Conclusion: Your Code Deserves Better
Python gives you the tools to write beautiful, efficient, and maintainable code. Using +
for string concatenation might feel intuitive, but it’s a performance trap hiding in plain sight. As your codebase grows and your data scales, these small inefficiencies snowball into real bottlenecks.
So next time you’re tempted to write:
result = s1 + s2 + s3
…stop, and ask yourself: Should I be using .join()
or an f-string instead?
Your future self — and your CPU — will thank you.
Write Pythonic code not just for correctness, but for clarity and performance. Don’t let lazy string joining slow you down.
