Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python语言引用在涉及字符串文本时是否犯了遗漏错误?_Python 3.x_String - Fatal编程技术网

Python 3.x Python语言引用在涉及字符串文本时是否犯了遗漏错误?

Python 3.x Python语言引用在涉及字符串文本时是否犯了遗漏错误?,python-3.x,string,Python 3.x,String,我一直在仔细研究Python语言参考,因为我正在编写一些Anki抽认卡。看起来我在检查字符串文字时遇到了一个障碍 请参见此处:函数调用print(“cant”)返回cant 但我在文档中找不到任何东西表明这是可能的 考虑一下这个正式定义的摘录: shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""'

我一直在仔细研究Python语言参考,因为我正在编写一些Anki抽认卡。看起来我在检查字符串文字时遇到了一个障碍

请参见此处:函数调用
print(“cant”)
返回
cant

但我在文档中找不到任何东西表明这是可能的

考虑一下这个正式定义的摘录:

shortstring     ::= "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring      ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
shortstringitem ::= shortstringchar | stringescapeseq
longstringitem  ::= longstringchar | stringescapeseq
shortstringchar ::= <any source character except "\" or newline or the quote>
longstringchar  ::= <any source character except "\">
stringescapeseq ::= "\" <any source character>`
shortstring::=“'”shortstringitem*“'”“|“'”shortstringitem*“'
longstring::=“longstringitem*”、“longstringitem*”、“longstringitem*”
shortstringitem::=shortstringchar | stringescapeseq
longstringitem::=longstringchar | stringescapeseq
shortstringchar::=


正如您可能知道的,序列
“cant”
是一个短字符串。这是因为它是由零个或多个短字符串项组成的一组,由匹配的双引号组包围

短字符串项可以是短字符串字符字符串转义序列。

由于序列
中没有字符串转义序列“can't”
,因此让我们看看短字符串。该定义说明引号(')不是短字符串

由于引号不是短字符串,因此
print('can')
抛出语法错误是有道理的。为什么
打印(“不能”)
不遵循相同的规则

我觉得Python语言参考在这里犯了一个遗漏的错误。想法?

由于
中的“不能”
以双引号开头,因此允许在文本中使用单引号

shortstringchar ::= <any source character except "\" or newline or the quote>
shortstringchar::=
“引号”是指打开字符串文字的字符(或)。这允许您在字符串文字中使用单引号或双引号(无需转义),方法是将文字括在相反的引号字符中