Converting integer to string in Python

Hick picture Hick · Jun 7, 2009 · Viewed 3.5M times · Source

I want to convert an integer to a string in Python. I am typecasting it in vain:

d = 15
d.str()

When I try to convert it to string, it's showing an error like int doesn't have any attribute called str.

Answer

Bastien Léonard picture Bastien Léonard · Jun 7, 2009
>>> str(10)
'10'
>>> int('10')
10

Links to the documentation:

Conversion to a string is done with the builtin str() function, which basically calls the __str__() method of its parameter.