Python concatenate string – with example

  • Reading time:16 mins read

Python concatenate strings – basics

Strings in Python are an array of bytes representing Unicode characters. In Python, strings are surrounded by double or single quotation marks. Unlike other programming languages, however, Python does not have a character data type.

 

Python concatenate strings – The join() method

String concatenation involves combining two strings together. There are multiple ways that we can use concatenate strings in Python. In this article, we will explore the three major ones. These include using the addition operator +, the multiplication operator * or using the join() method.

 

Python concatenate string

 

Using the + operator

It is one of the easiest methods that we can use to concatenate more than two strings. However, using this method we can only concatenate strings together, not any other data type. To demonstrate this we are going to create two variables each assigned to a string. We can concatenate the two together as shown below.

 

Concatenate empty spaces

Although we have managed to concatenate the two strings you’ll notice that the two are actually together side by side. However, sometimes we want a space between the two perhaps for readability. In that case, we are also going to all and empty space between the two strings as shown below.

 

Concatenate two strings directly

We can also opt to concatenate the two strings directly therefore there is no need to assign them to variables first. This will also work just fine and will return the same result.

 

Python concatenate strings –TypeErrors

As we mentioned in the beginning we can only concatenate strings together. This means that although the + operator is also used with numeric types in Python, we cannot use it to concatenate a string and any numeric type. In the example below we are trying to concatenate an integer and a string, this results in a TypeError.

 

Python concatenate strings – The str() function

However, if we really wish to concatenate a numeric type and a string, then in that case we have to convert the numeric type into a string using the str() function. 

 

Python concatenate strings – Enclose integers and floats in quote marks

Alternatively, we can also opt to enclose the integer or float that we want to concatenate in double or single quote marks. This will make the interpreter consider this integer as just another string.

 

 

Python concatenate strings – Using the * operator

In Python, this is referred to as the multiplication operator. However, its behaviour varies depending on the data type of the operands. When used with strings this operator appends the same string to itself according to the number of times specified. 

 

 

We can do this as many times as we wish the string to be repeated.

 

Python concatenate string

 

Using the .join() method

The join() method takes all items in an iterable and joins them together into one string.

We can also specify a separator that will separate the items when they are joined together.

In the example below we have created a list of animals, we are going to use the .join() method to join these strings into one.

 

 

We can also join the string items together without specifying any separator and instead have just a blank space that will concatenate them together without any separator as shown below.

 

Python concatenate strings – Iterating with loops

We can also opt to use a loop to iterate through the list and join the items together, however, the .join() method is more precise.

 

Python concatenate string

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

 

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.