可能比较 low 还是记录一下:
str 和 repr的使用过程
str 是一个类型 (int, long 类似), 同样她也可以作为一个工厂方法 实例一个 string
repr 是python 内置的函数, 用于保留一个 打印值在python 代码片段里的真实状态
好,以上全是废话
>>> a = 1
>>> a + ""
---------------------------------------------------------------------------
TypeError
Traceback (most recent call last)
<ipython-input-5-ebf3ab7f3a34> in <module>()
----> 1 a + ""
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> a = 1
>>> repr(a) + ""
可以看到, 我们可以通过使用 str 和 repr 来转换字符串
但是,str 只能提供一个 元string来做转换, 不可以是一个变量(她不具备执行变量的能力)
repr 是一个函数, 所以实际上是传参, 可以是变量和string