Concatenating strings refers to simply combining them together. In this tutorial, we’ll explore the different ways in which strings can be concatenated in Python with examples.
Table of Contents
- String concatenation in Python – using the
+
operator - Other methods for string concatenation in Python
- Using the
join()
function - Using the
%
operator - Using the
format()
function
- Using the
String concatenation in Python – using the +
operator
The most simple and preferred way to concatenate strings in python is by using the +
operator. The example below illustrates how to concatenate two strings.
# Concatenate two strings s1 = "World" s2 = "Cup" s3 = s1 + s2 print(s3)
The above code gives the following output on execution:
WorldCup
Using the +
operator we can concatenate any number of strings together. In the example below, we concatenate multiple strings together.
# Concatenate multiple strings s1 = "World" s2 = "Cup" s3 = "Winners!" s4 = s1 + " " + s2 + " " + s3 print(s4)
The above code gives the following output on execution:
World Cup Winners!
Note: Using the +
operator to concatenate a string and a numeric type results in an error. See the example below:
# Concatenate string with numeric type a = "Top" b = 5 c = a + b print(c)
The above code on execution gives a TypeError
------------------------------------------------------------------------ TypeError Traceback (most recent call last) <ipython-input-6-b252627bc7ca> in <module> 2 a = "Top" 3 b = 5 ----> 4 c = a + b 5 print(c) TypeError: can only concatenate str (not "int") to str
You can only concatenate string objects together. In order to concatenate a string and a number, you’ll have to first convert the number to a string object. This can be done using the str()
function.
# Concatenate string with numeric type a = "Top" b = str(5) c = a + b print(c)
The above code gives the following output on execution:
Top5
Other methods for string concatenation in Python
There are other ways as well to concatenate strings in python.
Using the join()
function
The python join()
function is used to concatenate strings in an iterable (example, list, tuple, etc). The example below illustrates how to combine strings using the join
function.
# concatenate strings using join() function st_list = ["World", "Cup", "Winners"] combined_string = " ".join(st_list) print(combined_string)
The above code gives the following output on execution:
World Cup Winners
In the above example, the list of strings to be concatenated, st_list
, is passed as an argument to the join()
function which joins them using a single space as a separator.
Using the %
operator
The %
operator is generally used for string formatting. However, it can also be used concatenating strings. It can be handy when we want to concatenate strings and perform simple formatting. The example below illustrates how to use the %
for concatenating strings in python.
# concatenate strings using the % operator a = "%s %s %s" % ("World", "Cup", "Winners") print(a)
The above code gives the following output on execution:
World Cup Winners
Using the format()
function
The format()
function in python is also used for string formatting purposes. It’s quite a robust function with different string formatting capabilities and can also be used for tasks like concatenating strings. The example below illustrates the use of format()
function for concatenating strings in python.
# concatenate strings using the format() function a = "{} {} {}".format("World", "Cup", "Winners") print(a)
The above code gives the following output on execution:
World Cup Winners
In the above example, the curly braces {}
act as placeholders which are filled with the values passed in the format()
function.
For more on the capabilities for the format()
function, refer to official python docs.
Other articles on python strings:
In this tutorial, we covered how to concatenate strings in python. If you found this article useful do give it a share! For more such articles subscribe to us.
If you’re a beginner looking to start your data science journey and learn python, check out our Python for Data Science Series.