Python: How to Convert Int to String

Converting numbers into string has many purposes such as formatting result in a data table, displaying big number, and combining number and string 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:

str(10234)
#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) + " "
print(result)
#output: 1 2 3 4 5

Leave a Comment

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

We care about your privacy and data security. Google serves cookies to analyse traffic and show ads on this site. By using this site you agree to these cookies being set. 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.

Close