8 Powerful Ways to Concatenate Strings in Python
This guide reviews eight common Python string concatenation techniques—including the plus operator, commas, direct literals, percent formatting, format(), f-strings, join(), and the multiplication operator—highlighting their syntax, use cases, performance considerations, and recommendations for small versus large string assemblies.
In Python there are multiple ways to concatenate strings, summarized here for reference.
Plus operator
First method uses the + operator:
>> a, b = 'hello', ' world'
>>> a + b
'hello world'Comma operator
Second method uses a comma, typically with print:
>> a, b = 'hello', ' world'
>>> print(a, b)
hello worldNote: using a comma creates a tuple when not printed.
>> a, b
('hello', ' world')Direct literals
Third method concatenates literals directly, with or without spaces:
print('hello' ' world')
print('hello''world')Percent formatting
Fourth method uses the % operator, the historic way to format strings:
print('%s %s' % ('hello', 'world'))format() method
Fifth method uses the format function:
print('{}{}'.format('hello', ' world'))join() method
Sixth method uses the built‑in join method on a sequence:
print('-'.join(['aa', 'bb', 'cc']))f‑string
Seventh method uses f‑strings (Python 3.6+), an evolution of % and format:
>> aa, bb = 'hello', 'world'
>>> f'{aa} {bb}'
'hello world'Multiplication operator
Eighth method repeats a string with the * operator:
>> aa = 'hello '
>>> aa * 3
'hello hello hello 'Summary
For a small number of strings , the + operator is recommended; if performance matters and you are on Python 3.6+, use f‑strings for better readability.
For a large number of strings , prefer join or f‑strings, depending on the Python version and readability needs.
Reference: “You don’t know Python | Secrets of string concatenation” – https://juejin.im/post/5b350624f265da5954426713
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
