Converting integer to string in Python
To manage non-integer inputs:
number = raw_input()
try:
value = int(number)
except ValueError:
value = 0
There is no typecast and no type coercion in Python. You have to convert your variable in an explicit way.
To convert an object into a string you use the str()
function. It works with any object that has a method called __str__()
defined. In fact
str(a)
is equivalent to
a.__str__()
The same if you want to convert something to int
, float
, etc.
>>> str(42)
'42'
>>> int('42')
42
Links to the documentation:
int()
str()
str(x)
converts any object x
to a string by calling x.__str__()
.
Try this:
str(i)