Python add strings

last modified February 18, 2021

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

Python tutorial is a comprehensive tutorial on Python language.

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 with 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 {} characers 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__.

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

Read Python tutorial or list all Python tutorials.