Converting integer to string in Python?

Go To StackoverFlow.com

1143

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.

2009-06-07 10:22
by Hick
So, did you try

d2=str(d)
c.append(d2[0])

is that what you want - nik 2009-06-07 10:43

use str(d) for getting string form of an integer - Arnab Ghosal 2012-01-13 11:04
http://blog.amir.rachum.com/post/48428590088/python-number-conversion-char - Amir Rachum 2013-04-22 19:59


1776

>>> str(10)
'10'
>>> int('10')
10

Links to the documentation:

The problem seems to come from this line: d.str().

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

2009-06-07 10:24
by Bastien Léonard
surprise, i will check type for false-true : UnicodeEncodeError: 'ascii' codec can't encode characters in position 332-333: ordinal not in range(128 - nerkn 2018-08-02 18:15


90

Try this:

str(i)
2009-06-07 10:23
by Lasse Vågsæther Karlsen
I like answers like this. Exactly answering the question based on the knowledge of the asker. Thank you - quemeful 2016-10-17 11:12


46

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.

2009-06-07 10:30
by Andrea Ambu


15

To manage non-integer inputs:

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

Ok, if I take your latest code and rewrite a bit to get it working with Python:

t=raw_input()
c=[]
for j in range(0,int(t)):
    n=raw_input()
    a=[]
    a,b= (int(i) for i in n.split(' '))
    d=pow(a,b)
    d2=str(d)
    c.append(d2[0])
for j in c:
    print j

It gives me something like:

>>> 2
>>> 8 2
>>> 2 3
6
8

Which is the first characters of the string result pow(a,b). What are we trying to do here?

2009-06-07 10:32
by nik


12

>>> 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
2009-06-07 10:24
by maxaposteriori


6

In Python => 3.6 you can use f formatting:

>>> int_value = 10
>>> f'{int_value}'
'10'
>>>
2018-07-20 13:55
by SuperNova
In fact, f formatting is faster than calling str() - user650654 2019-01-16 08:58


5

The most decent way in my opinion is ``.

i = 32   -->    `i` == '32'
2015-05-05 21:55
by Nikolas
Note that this is equivalent to repr(i), so it will be weird for longs. (Try i = `2 ** 32`; print i - NoName 2015-05-19 15:46
This has been deprecated in python 2 and completely removed in python 3, so I wouldn't suggest using it anymore. https://docs.python.org/3.0/whatsnew/3.0.html#removed-synta - teeks99 2015-07-13 18:47


3

Can use %s or .format

>>> "%s" % 10
'10'
>>>

(OR)

>>> '{}'.format(10)
'10'
>>>
2017-12-11 10:57
by SuperNova


3

For someone who wants to convert int to string in specific digits, the below method is recommended.

month = "{0:04d}".format(localtime[1])

For more details, you can refer to Stack Overflow question Display number with leading zeros.

2018-02-09 01:57
by Gearon