7 Efficient Ways to Concatenate Strings in Python
This article reviews seven common Python string‑concatenation techniques—including +, commas, direct literals, %, format(), join(), f‑strings, and *—and explains when each method is most suitable for small or large string assemblies.
In Python, there are multiple ways to concatenate strings; this guide provides a comprehensive summary for future reference.
Plus (+) Concatenation
Use the + operator to join strings.
a, b = 'hello', ' world'
print(a + b) # 'hello world'Comma Concatenation (print only)
Using a comma separates values in print, but assigning with a comma creates a tuple.
a, b = 'hello', ' world'
print(a, b) # hello world
# Assignment creates a tuple
(a, b) # ('hello', ' world')Direct Literal Concatenation
Place string literals next to each other; whitespace inside the literals is preserved.
print('hello' ' world')
print('hello''world')Percent (%) Formatting
Before Python 2.6, % was the primary string‑formatting operator and can also be used for concatenation.
print('%s %s' % ('hello', 'world'))format() Method
The format method, introduced in Python 2.6, replaces % for formatting and can concatenate strings.
print('{}{}'.format('hello', ' world'))join() Method
The built‑in join method concatenates an iterable of strings with a separator.
print('-'.join(['aa', 'bb', 'cc'])) # aa-bb-ccf‑string
Introduced in Python 3.6, f‑strings provide a concise way to embed expressions inside string literals, serving as an evolution of % and format.
aa, bb = 'hello', 'world'
print(f'{aa} {bb}') # hello worldMultiplication (*) Operator
The * operator repeats a string a given number of times.
aa = 'hello '
print(aa * 3) # hello hello helloConclusion
When concatenating a small number of strings
Prefer the + operator. If performance is critical and you are using Python 3.6+, f‑strings are recommended for better readability.
a = f'姓名:{name} 年龄:{age} 性别:{gender}'
b = '姓名:' + name + '年龄:' + age + '性别:' + genderWhen concatenating many strings
Use join or f‑strings, depending on your Python version and readability requirements.
Reference
You may not know the Python string concatenation secrets:
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.
