How to convert or concatenate integer with string variable in Python?

Convert int to str. Simple way to concatenate integer and string variables – Python. 

In some cases, you have to concatenate integer variables to string variables in your Python script. You can not do this by simply using the (+) sign. In this post I am going to explain the possibilities on how to concatenate integer with string variable in Python. For this you need to convert the integer to string first.

What are the ways to convert integer to string variable in Python?

  1. str()
  2. Using “
  3. repr()

Python is a good and simple language for SysAdmins automation tasks. See the Index page and read more about Python.

See more about Variables and Types – Python

I am using iPython for demonstration.

string = "Wiki's age is: "
age = 18

print (string + age)

Will get error. Please see the screenshot.
Int to str Python
Int to str Python

Solution

So, we need to convert the integer to string. Then we can print both as string! You can do it in different ways. Please see the examples pasted below:

How to Convert Int to String in Python?

1. Using function str()

string = "Wiki's age is: "
age = str(18)

print (string + age)
Int to str Python
Int to str Python

2. Using backtick (“)

Yeah, it’s possible to concatenate integer with string using backtick option. Please see the example added below:

age2 = 20

print (string + `age2`)
Wiki's age is: 20
Int to str Python
Int to str Python

In this example the first part is string and age2 is an integer variable.

3. Using function repr()

This is same as str() function. See the examples:

age3 = repr(21)

print (string + age3)
Wiki's age is: 21
Int to str Python
Int to str Python

That’s it!!

How to update a Docker image with new changes?

Yeah, we all know, the Docker image is the core part of your Docker container. The container works based on this Image. Docker image can be built using many ways.

We can build many containers from a single image. An image is a combination of a file system and parameters.

Once the container is launched using an image, you can make changes on that container. Like, you can create new files, you can install new modules, packages etc.. These changes will persist on the container as long as it exists. READ MORE..

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

4 thoughts on “How to convert or concatenate integer with string variable in Python?

  1. Awesome Article with nice explanation can you help me to store one string with one variable output to another variable like

    a = 4.5
    b = hello a
    print (b)

Leave a Reply

Your email address will not be published. Required fields are marked *