在 Python 中将整数转换为字符串?

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.

转载于:https://stackoverflow.com/questions/961632/converting-integer-to-string-in-python

csdnceshi75
衫裤跑路 blog.amir.rachum.com/post/48428590088/…
7 年多之前 回复
csdnceshi69
YaoRaoLov use str(d) for getting string form of an integer.
8 年多之前 回复
csdnceshi72
谁还没个明天 So, did you try d2=str(d) c.append(d2[0]) is that what you want?
大约 11 年之前 回复

10个回答

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

Also, it shouldn't be necessary to call pow(). Try using the ** operator.

weixin_41568126
乱世@小熊 surprise, i will check type for false-true : UnicodeEncodeError: 'ascii' codec can't encode characters in position 332-333: ordinal not in range(128)
大约 2 年之前 回复
csdnceshi71
Memor.の Just a note. This does not happen anymore. str(012) given SyntaxError:invalid token.
大约 2 年之前 回复
csdnceshi73
喵-见缝插针 did you hear the joke "Why do computer programmers confuse Halloween with Christmas?" Because Oct 31 = Dec 25. In C and many other languages a number start with 0 is also an octal number
大约 6 年之前 回复
csdnceshi76
斗士狗 because 0 is the prefix for octal representation in Python 2, like 0x is for hexadecimal (it's “0o” for octal in Python 3). So for str(), passing 012 is the same as passing 10.
6 年多之前 回复
csdnceshi70
笑故挽风 why does str(012) return '10' ?
6 年多之前 回复

Try this:

str(i)
csdnceshi54
hurriedly% I like answers like this. Exactly answering the question based on the knowledge of the asker. Thank you.
接近 4 年之前 回复

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.

a = 2

You can use str(a) which gives you a string object of int(2).

csdnceshi71
Memor.の clicks on Rambatino's profile whew! he's an iOS guy :)
接近 4 年之前 回复
csdnceshi73
喵-见缝插针 Why has this got up votes? It's just copied the original answer 4 years before!
4 年多之前 回复

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?

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

The most decent way in my opinion is ``.

i = 32   -->    `i` == '32'
csdnceshi75
衫裤跑路 This has been deprecated in python 2 and completely removed in python 3, so I wouldn't suggest using it anymore. docs.python.org/3.0/whatsnew/3.0.html#removed-syntax
大约 5 年之前 回复
csdnceshi63
elliott.david Note that this is equivalent to repr(i), so it will be weird for longs. (Try i = 2 ** 32; print i)
大约 5 年之前 回复

Can use %s or .format

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

(OR)

>>> '{}'.format(10)
'10'
>>>

In Python => 3.6 you can use f formatting:

>>> int_value = 10
>>> f'{int_value}'
'10'
>>>

对于想要将int转换为特定数字的字符串的人,建议使用以下方法。

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

有关更多详细信息,吗可以参考Stack Overflow问题。 显示数字的前导零.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问