Converting integer to string in Python

ID : 760

viewed : 392

Tags : pythonstringintegerpython





Top 5 Answer for Converting integer to string in Python

vote vote

98

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

vote vote

88

Try this:

str(i) 
vote vote

73

There is not typecast and no type coercion in Python. You have to convert your variable in an explicit way.

To convert an object in 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.

vote vote

66

To manage non-integer inputs:

number = raw_input() try:     value = int(number) except ValueError:     value = 0 
vote vote

59

>>> i = 5 >>> print "Hello, world the number is " + i TypeError: must be str, not int >>> s = str(i) >>> print "Hello, world the number is " + s Hello, world the number is 5 

Top 3 video Explaining Converting integer to string in Python







Related QUESTION?