How to Concatenate String and Int in Python?

One of my team members asked me how to concatenate string and int in Python. I explored and tested many methods to accomplish this task, and as a result, I found three efficient ways to concatenate strings and int in Python. In this tutorial, I will share my findings and help you to understand better with suitable examples and screenshots.

Concatenate String and Int in Python

Suppose you are building a customer management system for a company in the United States. You have customer names stored as strings and their corresponding IDs as integers. Now, you want to create a unique identifier for each customer by combining their name and ID. However, when you try to directly concatenate a string and an integer using the + operator, you encounter a TypeError. This is because Python does not allow direct concatenation of different data types.

For example, consider the following code snippet:

customer_name = "John Doe"
customer_id = 42

unique_identifier = customer_name + customer_id

Running this code will raise a TypeError: can only concatenate str (not "int") to str.

Read How to Check if String Length is Greater Than 0 in Python?

Solution 1: Use str() Method

One way to solve this problem is by converting the integer to a string using the built-in str() function in Python. By doing so, you can concatenate the string and the integer-turned-string using the + operator. Here’s an example:

customer_name = "John Doe"
customer_id = 42

unique_identifier = customer_name + str(customer_id)
print(unique_identifier) 

Output:

John Doe42

I have executed the above example code and added the screenshot below.

Concatenate String and Int in Python

As you can see, by wrapping the customer_id inside the str() function, we convert it to a string, allowing successful concatenation with the customer_name string.

Check out How to Check if a String is Comma Separated in Python?

Solution 2: Use f-Strings

If you are using Python 3.6 or later, you can leverage the power of f-strings to concatenate strings and integers seamlessly. F-strings provide a concise and readable way to embed expressions inside string literals. Here’s an example:

customer_name = "Jane Smith"
customer_id = 123

unique_identifier = f"{customer_name}{customer_id}"
print(unique_identifier) 

Output:

Jane Smith123

I have executed the above example code and added the screenshot below.

How to Concatenate String and Int in Python

In this approach, we prefix the string with an f and enclose the expressions inside curly braces {}. Python evaluates the expressions within the curly braces and replaces them with their string representations.

Read How to Check if a String is a GUID in Python?

Solution 3: Use the format() Method

Another approach is to use the format() method, which allows you to create a formatted string by replacing placeholders with actual values. Here’s an example:

customer_name = "Michael Johnson"
customer_id = 789

unique_identifier = "{}{}".format(customer_name, customer_id)
print(unique_identifier)  

Output:

Michael Johnson789

I have executed the above example code and added the screenshot below.

Concatenate String and Int in Python format()

In this case, we define a string with placeholders {} and then call the format() method, passing the customer_name and customer_id as arguments. The format() method replaces the placeholders with the corresponding values.

Check out How to Check if a String is in CamelCase Format Using Python?

Conclusion

In this tutorial, I have explained how to concatenate string and int in Python. I discussed important methods such as using the str() method, f-string, format() method.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.