How to Concatenate Strings in Python?

An infographic guide to mastering string concatenation in Python. Explore the power of +, join(), and string IO for dynamic text creation

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 covered in this guide to concatenate strings in Python:

  1. Using + (plus)
  2. Using += concatenate operator
  3. The join() method – For iterators
  4. Using StringIO
  5. 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 fewer strings to concatenate then you may use the ‘+’ operator.

You may use this operator as follows:

#A Demo of Python string concatenation

str_a = 'String'

str_b = 'Example'

str_c = str_a + ' Concatenation ' + str_b

print(str_c)

Output:

String Concatenation Example

The reason for using this approach for fewer strings is that as String objects are immutable i.e. once created they cannot be changed. So, each concatenation creates a new object.

Therefore, if you have many strings to combine then this is an inefficient way.

Using the ‘+=’ operator to concatenate two strings

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

#A Demo of string concatenation by +=

str_a = 'Concat '

str_b = 'Example'

str_a += str_b

print(str_a)

Output:

Concat Example

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, tuples, 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 the join() method is used to return a concatenated string:

#String concatenation by join with list

lst_str = ['String', 'Concat', 'by', 'Join', 'Method']

str_concat = " ".join(lst_str)

print ("After join: ", str_concat)

Output:

After join: String Concat by Join Method

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 be produced as shown in the example below:

Python code:

lst_str = ['List', 'with', 10, 'Mixed', 'Items', 25]



str_concat = " ".join(lst_str)

print ("After join: ", str_concat)

Output:

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 the IO module. See the following example of using StringIO for combining strings:

from io import StringIO


concat = StringIO()


concat.write('String ')

concat.write('Concat ')

concat.write('by ')

concat.write('StringIO')


print(concat.getvalue())

Output:

String Concat by 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 and above.

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 the least required but possible:

#Concatenation of string literals


print ('Python '  'Simple ' 'Concatenation')

Output:

Python Simple Concatenation

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 strings. For large numbers of strings, either use the join() method or StringIO – the official recommended way for efficiency.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!