Tutorial

How To Concatenate String and Int in Python

Updated on September 21, 2022
author

Pankaj

How To Concatenate String and Int in Python

Introduction

Python supports string concatenation using the + operator. In most other programming languages, if we concatenate a string with an integer (or any other primitive data types), the language takes care of converting them to a string and then concatenates it.

However, in Python, if you try to concatenate a string with an integer using the + operator, you will get a runtime error.

Example

Let’s look at an example for concatenating a string (str) and an integer (int) using the + operator.

string_concat_int.py
current_year_message = 'Year is '

current_year = 2018

print(current_year_message + current_year)

The desired output is the string: Year is 2018. However, when we run this code we get the following runtime error:

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in <module>
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str

So how do you concatenate str and int in Python? There are various other ways to perform this operation.

Prerequisites

In order to complete this tutorial, you will need:

This tutorial was tested with Python 3.9.6.

Using the str() Function

We can pass an int to the str() function it will be converted to a str:

print(current_year_message + str(current_year))

The current_year integer is returned as a string: Year is 2018.

Using the % Interpolation Operator

We can pass values to a conversion specification with printf-style String Formatting:

print("%s%s" % (current_year_message, current_year))

The current_year integer is interpolated to a string: Year is 2018.

Using the str.format() function

We can also use the str.format() function for concatenation of string and integer.

print("{}{}".format(current_year_message, current_year))

The current_year integer is type coerced to a string: Year is 2018.

Using f-strings

If you are using Python 3.6 or higher versions, you can use f-strings, too.

print(f'{current_year_message}{current_year}')

The current_year integer is interpolated to a string: Year is 2018.

Conclusion

You can check out the complete Python script and more Python examples from our GitHub repository.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
December 31, 2021

num = ([3,4,3,2,1]) print(reduce(lambda x,y: x * 10 + y ,num)) 34321

- Egmon

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 8, 2021

    it works fine but takes too much time for instance if we have to convert integer to string 200 times , you would be able to notice the time taken to execute the code. How can we deal with that to make our code more efficient?

    - future googler

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      July 18, 2020

      Write a python program that asks the user how many coins of various types they have, and then prints the total amount of money in rupees.

      - M P ANUPAMA

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        February 17, 2019

        Hey Pankaj, thanks for sharing this article. This helped me to have a better understanding of formats used with print function

        - Anupinder singh

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Featured on Community

          Get our biweekly newsletter

          Sign up for Infrastructure as a Newsletter.

          Hollie's Hub for Good

          Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

          Become a contributor

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          Welcome to the developer cloud

          DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

          Learn more