Python 字符串文字以单引号开始,以三引号结束,它如何有效?

Python 字符串文字以单引号开始,以三引号结束,它如何有效?,python,Python,Q1)。如果字符串以单引号开始,以三引号结束,它如何有效 问题2)。如果第二条打印语句有效,为什么第三条和第四条打印语句无效 print('This is "python" programming') #valid print('This is "python" programming''') #valid print(''This is "python" programming''') #Invalid print('''This is "python" programming') #Inva

Q1)。如果字符串以单引号开始,以三引号结束,它如何有效

问题2)。如果第二条打印语句有效,为什么第三条和第四条打印语句无效

print('This is "python" programming') #valid
print('This is "python" programming''') #valid
print(''This is "python" programming''') #Invalid
print('''This is "python" programming') #Invalid
在python中,您可以

例如:
“a”“b”
,甚至在多行上:

re.compile("[A-Za-z_]"       # letter or underscore
           "[A-Za-z0-9_]*"   # letter, digit or underscore
          )
所以
'这是“python”编程'
'这是“python”编程'
''
的串联

三重引号(
'
'
)是另一种类型的引号,用于在多行上具有字符串跨度

从python手册:

字符串文字可以跨多行。一种方法是使用三重引号:
“…”
“…”
。行尾自动包含在字符串中,但可以通过在行尾添加\来防止这种情况

第三个示例实际上是无效的,因为它是空字符串,后跟对python没有意义的标识符

最后一个示例无效,因为它以三重引号开始,但没有关闭三重引号