>>> arr = [ 'asdf' 'asdfsdgf' ]
>>> print arr
['asdfasdfsdgf']
为什么存在隐式字符串连接而不是SyntaxError
?
>>> arr = [ 'asdf' 'asdfsdgf' ]
>>> print arr
['asdfasdfsdgf']
为什么存在隐式字符串连接而不是SyntaxError
?
相邻的字符串在 Python 中连接:
>>> "Happy " "Birthday!"
'Happy Birthday!'
>>>
这只是语法。您需要在列表中使用逗号来分隔项目:
>>> arr = ["asdf", "asdfsdgf"]
>>> # --^
>>> arr
['asdf', 'asdfsdgf']
隐式字符串连接允许您将长字符串拆分为多行以提高可读性:
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 个元素。