Python: How to Convert Int to String

Converting numbers into strings has many purposes such as formatting results in a data table, displaying big numbers, and combining numbers and strings for displaying. Python provides str() function for this task, which basically calls the __str__() method of its parameter. The function will help users avoid encountering “TypeErrors” when converting int or float to a string when coding in Python.

Open any Python editors and follow these examples:

#output: '10234'

years = 5
print("I bought this car " + str(years) + " years ago.")
#output: 'I bought this car 5 years ago.

result = ""
for i in range(1, 5):
result += str(i) + " "
#output: 1 2 3 4 5

Leave a Comment

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

Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.