Which is the preferred way to concatenate a string in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
There is no single best way to concatenate strings in Python for every situation. The preferred approach depends on whether you are combining a few known values, formatting variables into text, or joining many pieces in a loop.
Use + For A Small Number Of Pieces
If you are combining just two or three strings, + is clear and perfectly fine.
This is readable and idiomatic for small cases. The problem starts when repeated concatenation happens inside a loop, because each concatenation creates a new string.
Use F-Strings For Formatting
If the goal is to build a readable sentence from values, f-strings are usually the preferred option.
F-strings are often the best answer when variables need to be embedded in text. They are concise, readable, and support formatting rules.
This is not just concatenation. It is formatting plus string construction, which is why f-strings are usually the cleanest tool here.
Use ''.join(...) For Many Pieces
When you already have an iterable of strings, join is the preferred approach.
This is especially important in loops.
Bad pattern:
Better pattern:
join is preferred here because it builds the final string in one step instead of allocating a new string during each iteration.
Prefer Simplicity Before Micro-Optimization
Python does optimize some simple concatenation cases, and for very small strings the performance difference may not matter. That means style should usually follow intent:
- use
+for a few literal pieces - use f-strings for formatted text
- use
joinfor many items or loops
This rule is simple, practical, and easy to explain in code review.
print Arguments Are Not Concatenation
Sometimes you do not need concatenation at all.
This prints values separated by spaces. It is fine for terminal output, but it does not produce a reusable string value.
If you need an actual string for logging, storage, or further processing, build the string explicitly.
Be Careful With Non-String Values
join requires strings. If your iterable contains integers or other objects, convert them first.
This is another place where f-strings or comprehensions are often clearer than repeated manual conversion.
Common Pitfalls
The most common mistake is using += inside a long loop when join is the better tool.
Another mistake is using concatenation when formatting is what you actually need. F-strings are usually clearer for that.
Developers also sometimes pass non-string values into join and then wonder why it fails.
Finally, do not over-optimize tiny concatenations. For a couple of strings, + is completely reasonable.
Summary
- Use
+for a small number of string pieces. - Use f-strings when inserting values into text.
- Use
''.join(...)when combining many strings or loop-generated pieces. - Convert non-string items before using
join. - Prefer the method that matches the intent of the code.
Related reading
- Which Python memory profiler is recommended?
- Which Python packages offer a stand-alone event system?
- Which version of Python do I have installed?
- Why am i getting AttributeError 'KerasClassifier' object has no attribute 'model'?
- Why am I getting Permission denied when activating a venv?
- Why am I having KeyError 'val_acc'?
- Why am I seeing TypeError string indices must be integers?
- Why are Python's arrays slow?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.