今天遇到了一些有趣的Python行为。我以为我在写作print("{}".format("some value"))
但是我写了
^{pr2}$
有趣的是它起作用了。所以我的问题是,这是怎么工作的?在
深入挖掘
这种行为似乎是Python特有的。在
Python2.7>>> print("{}").format("testing")
testing
Python3.4>>> print("{}").format("testing)
File "", line 1
print("{}").format("testing)
^
SyntaxError: EOL while scanning string literal
python2的print函数似乎没有返回值,但是Python3有返回值?这让我更加困惑。在
Python2.7>>> type(print("testing))
File "", line 1
type(print("testing))
^
SyntaxError: invalid syntax
>>> a = print("testing")
File "", line 1
a = print("testing")
^
SyntaxError: invalid syntax
Python3.4>>> type(print("{}"))
{}
>>> a = print("{}")
{}
>>> a
>>> type(a)