How to concatenate strings in Python?

In Python, you may concatenate strings in different ways. Which way to use depends on the scenario or string source.

Following are the ways to concat strings in Python:

  • Using + (plus)
  • Using += concatenate operator
  • The join() method – For iterators
  • Using StringIO
  • The space between string literals

All of these approaches are explained below with example code and details.

An example of + operator to combine strings

If you have a fewer string to concatenate then you may use the + operator. The reason is, as String objects are immutable i.e. once created cannot be changed. So, each concatenation creates a new object. Therefore, if you have many strings to combine then this is an inefficient way.

However, for a few strings you may use this operator as follows:

See online demo and code

concat plus

Using the += operator to concatenate two strings

In this example, two string objects are concatenated by using the += operator. Have a look:

See online demo and code


String concatenation in Python using join() example

This is the efficient way of concatenating strings in Python if you have many strings. However, the join() method works with iterators like lists, tuple, etc.

The official recommendation is to place many strings into a list and then use the join() method to combine them by using a separator as shown in the example below. A list of a few string elements is created and join() method is used to return a concatenated string:

See online demo and code

string concatenation

For your information, the join method is used to concatenate a sequence by a given separator like space, comma etc. For more information on join() method visit this tutorial.

Note: The list must contain all string elements. If a number like int is found, a TypeError will produce as shown in the example below:

See online demo and code

string concat error

Tip: You may convert the numbers into strings in a sequence to avoid errors by using the str method.

A demo of StringIO for concatenating strings

The other recommended way for efficiently concatenating string objects is using the StringIO from IO module. See the following example of using StringIO for combining strings:

See online demo and code

string concatenation StringIO

Again, this is suitable if you have many strings to concatenate. You may learn more about StringIO here.

Note: This example applies to Python 3.

Concatenating string literals example

The last example is pretty simple; if you have a few string literals to concatenate then just use a space. Well, this apparently is least required but possible:

See online demo and code

string literal concat

Summary of concatenating strings in Python

In this tutorial, five ways of string concatenation in Python are explained with examples. You should choose + or += for the small number of string. For large numbers, either use the join() method or StringIO – the official recommended way for efficiency.