Fundamentals 4 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
7 Efficient Ways to Concatenate Strings in Python

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-cc

f‑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 world

Multiplication (*) Operator

The * operator repeats a string a given number of times.

aa = 'hello '
print(aa * 3)  # hello hello hello

Conclusion

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 + '性别:' + gender

When 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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceJOINString concatenationf-stringformat
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.