Strange valid python syntax with string [duplicate]

戏子无情 提交于 2020-01-30 10:31:26

问题


I am running on Python 3.6.1 and today I missed a comma, as follows:

nt = namedtuple('Record', ['c', 'a' 'b'])
# instead the following is what I actually want
nt = namedtuple('Record', ['c', 'a', 'b'])

But I just wonder why the first way is valid Python in any way? Should not it complains with syntax error?

I just tried in 3.5.2 and 2.7.11. Seems all valid. But it is valid?


回答1:


From the Python tutorial:

Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.

>>> 'Py' 'thon'
'Python'



回答2:


Python will concatenate adjacent strings which are delimited by whitespace: https://docs.python.org/2.0/ref/string-catenation.html



来源:https://stackoverflow.com/questions/50698086/strange-valid-python-syntax-with-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!