Comprehensive Guide to String Concatenation Methods in Python
This article presents a comprehensive overview of seven Python string concatenation techniques—including the plus operator, comma printing, implicit literal concatenation, percent formatting, format method, join function, f‑strings, and the multiplication operator—along with performance tips and usage recommendations.
Python offers many ways to concatenate strings. This guide summarizes the most common methods and provides practical examples, helping developers choose the most suitable approach for their code.
1. Plus Operator (+)
Use the + symbol to join two strings and produce a new string.
<code>>> a, b = 'hello', ' world'
>>> a + b
'hello world'</code>2. Comma in print
Placing a comma between values in print prints them with a space, but the comma creates a tuple when used in an assignment.
<code>>> a, b = 'hello', ' world'
>>> print(a, b)
hello world</code>Note: a, b without print results in the tuple ('hello', ' world') .
3. Implicit Literal Concatenation
Adjacent string literals are automatically concatenated by the interpreter; no operator is needed.
<code>print('hello' ' world')
print('hello''world')</code>This is a compile‑time optimization: only one string object is created.
4. Percent Operator (%)
Before Python 3.6, % was the primary string‑formatting operator and can also be used for concatenation.
<code>print('%s %s' % ('hello', 'world'))</code>5. format Method
The format method, introduced in Python 2.6, replaces the percent operator for formatting and can concatenate strings as well.
<code>print('{}{}'.format('hello', ' world'))</code>6. join Method
The built‑in join method concatenates an iterable of strings using a separator.
<code>print('-'.join(['aa', 'bb', 'cc']))</code>7. f‑Strings
Available from Python 3.6, f‑strings (formatted string literals) provide a concise and readable way to embed expressions directly in strings.
<code>>> aa, bb = 'hello', 'world'
>>> f'{aa} {bb}'
'hello world'</code>8. Multiplication Operator (*)
The * operator repeats a string or sequence; it is implemented via the magic method __mul__ .
<code>>> aa = 'hello '
>>> aa * 3
'hello hello hello '</code> <code>>> a = [1]
>>> a * 2
[1, 1]
>>> a.__mul__(3)
[1, 1, 1]</code>Conclusion
For a small number of strings, the + operator is simple and readable. When performance matters and you are using Python 3.6+, f‑strings are recommended for their clarity. For concatenating many strings, join (or f‑strings in suitable cases) provides the best efficiency.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.