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.

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.

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.

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”

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’.

Last Lines

Python is one of the most famous programming languages along with java. Changing the variable type on Python one of the primary techniques. When you learn those, you can go for advanced options also.

 

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

Leave a Reply

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