How to Concatenate Strings and Ints in Python

In this tutorial, we will explore how to concatenate strings and integers in Python. Concatenating refers to the process of combining two strings or variables into one. It’s an essential coding task in Python especially because it makes the programming process easy and simplified. So, without further ado, let’s jump right into it.

Step 1: Concatenating Two Strings

Concatenating two strings in Python is as simple as using the “+” operator. You can do this as shown below:

Output:

Hello World

Step 2: Converting Integer to String

In Python, you cannot concatenate a string with an integer directly. You need to convert the integer into a string first. You can achieve this by using Python’s built-in str() function. The str function converts a specified value into a string.

Output:

123

Step 3: Concatenating Strings and Integers

Now that you have the integer value as a string, you can concatenate it with a different string as shown below:

Output:

Hi 123

Full code:

Before proceeding to the conclusion, here is the complete code used in this tutorial:

Output:

Hello World
123
Hi 123

Conclusion

Concatenating strings and integers in Python is fairly simple and straightforward. Most operations in Python that involve a string and an integer require that the integer is converted into a string first. This is achieved using the in-built str() function.