如何将整数转换为字符串?

42   ⟶   "42"

相反,请参见How do I parse a string to a float or int?。浮点数可以类似地处理,但处理小数点可能很棘手,因为浮点值不精确。有关更多具体建议,请参阅将浮点转换为字符串而不舍入。


当前回答

您可以使用%s或.format:

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

Or:

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

其他回答

试试看:

str(i)

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

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

您可以使用%s或.format:

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

Or:

>>> '{}'.format(10)
'10'
>>>
>>> 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

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

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