How to Convert Python Int to String

Python is one of the most used programming languages in today’s world. In Python when you try to display an integer value with a string, you will get an error saying, ‘TypeError: Can’t convert ‘Int’ object to str implicitly’. It’s one of the simple but big problems in Python. But, there is a way to solve this problem. So, how to convert Python Int to String? Here you will find the answer.

Advertisement

 Read also: How to Setup an Omegle Bot 

How to Convert Python Int to String

To convert Python Int to String on your program, you will need to follow a simple trick. Here you can use a special code that is ‘str()’. It’s a magical code that turns Python Int to String easily. In between those parentheses, you just have to put the Python Int value and your numerical value will be displayed as a string. You can also concatenate your numerical value with a string output. Here’s how you can do it.

Step 1: Open Python editor on your computer device.

Step 2: Now type ‘str(number)’ on your desired Python code.

Advertisement

Note: Put your number value in between those parentheses.

Step 3: Now run your Python code and your given Int value will be displayed as a String value.

 Read also: How to Share Audio on Skype 

Example

As an example, we will display our numerical value in string format. First, think that we have a fitness-related software. Here we will tell that our program has measured and display ‘You have run 17 kilometers this week’. The code is given here.
[code language=’python’]

user = “Thomas”

Advertisement

km = 17

print(“Hey, ” + user + “! You ran ” + str(km) + ” kilometers in this week.”)

Output: Hey, Thomas! You ran 17 kilometers in this week.

Remember,
👉If you don’t put ‘str(km)’ in this code, then you will get an error named ‘TypeError’.

 Read also: Where are Google Chrome Bookmarks Stored 

So, hope you have learned, how to covert Python Int to String.

What your opinion on this trick? Have you loved?

If you have any problem with this method, please comment here.

Source:
https://docs.python.org/2.4/lib/typesnumeric.html

Advertisement

Torat Hossain, senior author in bNewTech. I love to learn new things and help people. I love Science, Technology, Finance, Games, and others.

Bnewtech.com