Skip to content
8 Powerful Ways to Concatenate Strings in Python
Fundamentals 4 min read

8 Powerful Ways to Concatenate Strings in Python

This article presents a comprehensive overview of eight different techniques for joining strings in Python, covering simple operators, formatting methods, built‑in functions, and best‑practice recommendations for both small and large concatenations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations

In Python, there are many ways to concatenate strings, and this article provides a comprehensive overview.

Plus (+) concatenation

Use the + operator:

>> a, b = 'hello', ' world'
>>> a + b
'hello world'

Comma (,) concatenation

Use a comma, which works only with print and creates a tuple when assigned:

>> a, b = 'hello', ' world'
>>> print(a, b)
hello  world
>> a, b
('hello', ' world')

Direct concatenation

Simply place string literals together (with or without spaces):

print('hello' ' world')
print('hello''world')

Percent (%) formatting

Before Python 2.6, % was the only 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 % and can concatenate strings:

print('{}{}'.format('hello', ' world'))

join() method

The built‑in join method concatenates elements of a sequence:

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'

Star (*) operator

Repeating a string with the * operator:

>> aa = 'hello '
>>> aa * 3
'hello hello hello '

Summary

When concatenating a small number of strings

Prefer the + operator. If performance matters and you are on Python 3.6+, use f‑strings for better readability:

a = f'Name:{name} Age:{age} Gender:{gender}'
b = 'Name:' + name + ' Age:' + age + ' Gender:' + gender

When concatenating many strings

Use join or f‑strings, depending on the Python version and readability requirements.

Reference

Source: “You don’t know Python | The secret of string concatenation” – 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.

PythonjoinString Concatenationf-stringformat
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.