7 Powerful Ways to Concatenate Strings in Python
This article reviews seven common Python string concatenation techniques—including the plus operator, commas, direct literals, the % operator, format(), f-strings, join(), and the multiplication operator—highlighting their syntax, use cases, and performance considerations for both small and large strings.
In Python there are multiple ways to concatenate strings, and this guide provides a comprehensive summary for future reference.
Plus operator
Use the + operator:
a, b = 'hello', ' world'
a + b # 'hello world'Comma operator
Use a comma, which works in print statements but creates a tuple when used in assignments:
a, b = 'hello', ' world'
print(a, b) # hello world a, b # ('hello', ' world')Direct literals
Simply place string literals next to each other (with or without spaces):
print('hello' ' world')
print('hello''world')Percent operator
Before Python 2.6 the % operator was the only formatting method and can also concatenate strings:
print('%s %s' % ('hello', 'world'))format() method
The format method, introduced in Python 2.6, replaces % and can concatenate strings:
print('{}{}'.format('hello', ' world'))join() method
The built‑in join method concatenates an iterable of strings:
print('-'.join(['aa', 'bb', 'cc']))f‑string
Python 3.6 introduced formatted string literals (f‑strings), an evolution of % and format:
aa, bb = 'hello', 'world'
f'{aa} {bb}' # 'hello world'Multiplication operator
Use * to repeat a string:
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 your Python version and readability requirements.
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.
