求解:将其他数据类型转化成字符串时出错
如图,转化成其他类型没毛病,就是转化成字符串时报错 应该是你上面定义了一个str的变量,和内置函数str重名导致的 >>> a = '520'>>> int(a)
520
>>> float(a)
520.0
>>> str(a)
'520'
>>> 不论你a赋的是数字还是字符串,转换为字符串都有个引号 正确的。可能是你的版本的问题。我使用2.7.14及3.6.5均正常运行。运行情况如下:
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) on win32
Type "copyright", "credits" or "license()" for more information.
>>> a = 520
>>> str(a)
'520'
>>> a = '520'
>>> int(a)
520
>>> float(a)
520.0
>>>
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) on win32
Type "copyright", "credits" or "license()" for more information.
>>> a = 520
>>> str(a)
'520'
>>> a = '520'
>>> int(a)
520
>>> float(a)
520.0
>>>
页:
[1]