Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.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/7/user-interface/2.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三重字符串引号声明_Python_String - Fatal编程技术网

Python三重字符串引号声明

Python三重字符串引号声明,python,string,Python,String,我使用三重字符串的方式如下: str="""jeff""" str=""""jeff""" str=""""jeff"""" # error str=""""jeff """" 第三个是错误,有人能解释一下为什么这是错误吗?只使用3个引号 第二个字符串被解释为:jeff 第三个字符串被解释为:jeff,后跟一个不正确的引号。仅使用3个引号 第二个字符串被解释为:jeff 第三个字符串被解释为:jeff,后跟一个不正确的引号。三个引号终止一个字符串,因此 str=""""jeff""""

我使用三重字符串的方式如下:

str="""jeff"""
str=""""jeff"""
str=""""jeff""""   # error
str=""""jeff """"
第三个是错误,有人能解释一下为什么这是错误吗?

只使用3个引号

第二个字符串被解释为:jeff

第三个字符串被解释为:jeff,后跟一个不正确的引号。

仅使用3个引号

第二个字符串被解释为:jeff


第三个字符串被解释为:jeff,后跟一个不正确的引号。

三个引号终止一个字符串,因此

str=""""jeff""""
解析为:

str= """ ("jeff) """ (")
后面的引号就是问题所在

顺便说一句,看看

很明显,星*是非贪婪的,但我不知道这是否有记录在案

作为对评论的回应,这

 str = ''''''''jeff'''
被解释为

(''')(''')('')(jeff)(''') <-- error, two quotes
 str = (''')(''')(''')(jeff)(''') <-- no error, empty string + jeff
被解释为

(''')(''')('')(jeff)(''') <-- error, two quotes
 str = (''')(''')(''')(jeff)(''') <-- no error, empty string + jeff

三个引号终止一个字符串,因此

str=""""jeff""""
解析为:

str= """ ("jeff) """ (")
后面的引号就是问题所在

顺便说一句,看看

很明显,星*是非贪婪的,但我不知道这是否有记录在案

作为对评论的回应,这

 str = ''''''''jeff'''
被解释为

(''')(''')('')(jeff)(''') <-- error, two quotes
 str = (''')(''')(''')(jeff)(''') <-- no error, empty string + jeff
被解释为

(''')(''')('')(jeff)(''') <-- error, two quotes
 str = (''')(''')(''')(jeff)(''') <-- no error, empty string + jeff
str=jeff->str'jeff'

str=jeff->多行str'jeff'

str=jeff error->这里解析器认为您声明了,jeff

str=jeff错误->与上一个相同

>>> """"a""""
  File "<stdin>", line 1
    """"a""""
            ^
SyntaxError: EOL while scanning string literal
>>> """"a """"
  File "<stdin>", line 1
    """"a """"
             ^
SyntaxError: EOL while scanning string literal
要避免它,请执行以下操作\a\

此外,如前所述,您可以查看BNF

str=jeff->str'jeff'

str=jeff->多行str'jeff'

str=jeff error->这里解析器认为您声明了,jeff

str=jeff错误->与上一个相同

>>> """"a""""
  File "<stdin>", line 1
    """"a""""
            ^
SyntaxError: EOL while scanning string literal
>>> """"a """"
  File "<stdin>", line 1
    """"a """"
             ^
SyntaxError: EOL while scanning string literal
要避免它,请执行以下操作\a\

此外,如前所述,您可以在BNF中查看本例str=jeff'中发生的情况,它只有3个尾随引号,仍然是一个错误本例str=jeff'中发生的情况,它只有3个尾随引号,仍然是一个错误