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:
- 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 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)
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)
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)
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.
lst_str = ['List', 'with', 10, 'Mixed', 'Items', 25] str_concat = " ".join(lst_str) print ("After join: ", str_concat)
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())
Again, this is suitable if you have many strings to concatenate. You may learn more about StringIO here.
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')
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.