我最近开始学习Python,我不明白Python为什么会这样:
>>> "OK" 'OK' >>> """OK""" 'OK' >>> "not Ok' File "<stdin>", line 1 "not Ok' ^ SyntaxError: EOL while scanning string literal >>> "not OK""" 'not OK'
为什么它不给最后一个语句一个错误,因为引号的数量不匹配?
我最近开始学习Python,我不明白Python为什么会这样:
>>> "OK" 'OK' >>> """OK""" 'OK' >>> "not Ok' File "<stdin>", line 1 "not Ok' ^ SyntaxError: EOL while scanning string literal >>> "not OK""" 'not OK'
为什么它不给最后一个语句一个错误,因为引号的数量不匹配?
最后的"""
不被认为是三重引号,而是一个"
(用于关闭当前字符串文字),后跟一个空字符串""
;这两个并置的字符串文字是串联的.通过在关闭和打开"
之间留出空间,可以更容易地识别相同的行为.
>>> "not OK" ""
'not OK'