Python add strings

last modified January 29, 2024

Python add string tutorial shows how to concatenate strings in Python.

In Python, a string is an ordered sequence of Unicode characters.

There are several ways of adding strings in Python:

Python add strings with + operator

The easiest way of concatenating strings is to use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded.


a = 'old'
b = ' tree'

c = a + b

Two strings are added using the + operator.

$ ./add_string.py 
old tree

In the second example, we use the compound addition operator.


msg = 'There are'

msg += ' three falcons'
msg += ' in the sky'


The example builds a message with the += operator.

$ ./add_string2.py 
There are three falcons in the sky

Python add strings with join

The string join method concatenates any number of strings provided in an iterable (tuple, list). We specify the character by which the strings are joined.


msg = ' '.join(['There', 'are', 'three', 'eagles', 'in', 'the', 'sky'])

In the example, we form a message by joining seven words. The words are joined with a single space character.

$ ./add_string_join.py
There are three eagles in the sky

Python add strings with string formatting

We can build Python strings with string formatting. The variables are expanded in the {} characters inside the string.


w1 = 'two'
w2 = 'eagles'

msg = f'There are {w1} {w2} in the sky'

We build a message with Python's fstring.

$ ./format_str.py 
There are two eagles in the sky

Python add strings with __add_ method

Another possibility to add strings is to use the special __add__ dunder method.


s1 = "and old"
s2 = " falcon"

s3 = s1.__add__(s2)

The example adds two strings with __add__.


Python common string operations

In this article we have shown several ways to add strings in Python.


My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Python tutorials.