1
>>> arr  = [ 'asdf' 'asdfsdgf' ]
>>> print arr
['asdfasdfsdgf']

为什么存在隐式字符串连接而不是SyntaxError?

4

2 回答 2

6

相邻的字符串在 Python 中连接:

>>> "Happy " "Birthday!"
'Happy Birthday!'
>>>

这只是语法。您需要在列表中使用逗号来分隔项目:

>>> arr  = ["asdf", "asdfsdgf"]
>>> #           --^
>>> arr
['asdf', 'asdfsdgf']
于 2013-10-04T15:22:20.947 回答
0

隐式字符串连接允许您将长字符串拆分为多行以提高可读性:

list_of_strings = [ "really long line "           # Start of first string
                    "to split for readability",  # Continuation of first string
                    "short string 1",
                    "short string 2"
                  ]

list_of_strings包含 3 个元素。

于 2013-10-04T16:24:22.160 回答