问题:将整数转换为字符串?

我想在Python中将整数转换为字符串。我是徒劳地打字:

d = 15
d.str()

当我尝试将其转换为字符串时,它显示错误,例如int没有任何名为的属性str

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.


回答 0

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

链接到文档:

转换为字符串是通过内置str()函数完成的,该函数基本上调用__str__()其参数的方法。

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


回答 1

尝试这个:

str(i)

Try this:

str(i)

回答 2

Python中没有类型转换,也没有类型强制。您必须以显式方式转换变量。

要使用字符串转换对象,请使用str()函数。它适用于具有称为__str__()define 的方法的任何对象。事实上

str(a)

相当于

a.__str__()

如果要将某些内容转换为int,float等,则相同。

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.


回答 3

要管理非整数输入:

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

To manage non-integer inputs:

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

回答 4

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

回答 5

在Python => 3.6中,您可以使用f格式:

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

In Python => 3.6 you can use f formatting:

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

回答 6

对于Python 3.6,您可以使用f-strings新功能将其转换为字符串,并且与str()函数相比,它更快,它的用法如下:

age = 45
strAge = f'{age}'

因此,Python提供了str()函数。

digit = 10
print(type(digit)) # will show <class 'int'>
convertedDigit= str(digit)
print(type(convertedDigit)) # will show <class 'str'>

有关更多详细的答案,请查看本文:将Python Int转换为String并将Python String转换为Int

For Python 3.6 you can use the f-strings new feature to convert to string and it’s faster compared to str() function, it is used like that:

age = 45
strAge = f'{age}'

Python provides the str() function for that reason.

digit = 10
print(type(digit)) # will show <class 'int'>
convertedDigit= str(digit)
print(type(convertedDigit)) # will show <class 'str'>

For more detailed answer you can check this article: Converting Python Int to String and Python String to Int


回答 7

我认为最体面的方式是“。

i = 32   -->    `i` == '32'

The most decent way in my opinion is “.

i = 32   -->    `i` == '32'

回答 8

可以使用%s.format

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

(要么)

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

Can use %s or .format

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

(OR)

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

回答 9

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

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

有关更多详细信息,您可以参考堆栈溢出问题显示数字前导零

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.


回答 10

通过在Python 3.6中引入f字符串,这也将起作用:

f'{10}' == '10'

实际上str(),它比调用速度更快,但会降低可读性。

实际上,它比%x字符串格式和.format()!快。

With the introduction of f-strings in Python 3.6, this will also work:

f'{10}' == '10'

It is actually faster than calling str(), at the cost of readability.

In fact, it’s faster than %x string formatting and .format()!


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。