在Python中,在这种情况下,不同的引号意味着什么?

在Python中,在这种情况下,不同的引号意味着什么?,python,quotation-marks,Python,Quotation Marks,我原以为我知道不同类型的Python引用的含义,但被这个示例弄糊涂了: my_string = 'the cat sat on the mat' my_string = "the cat sat on the mat" my_string = """the cat sat on the mat""" my_string = '''the cat sat on the mat''' 前两种似乎是使用单引号或双引号声明字符串的两种不同方式。最后两个似乎是使表达式不完整的注释(并将在解释器中生

我原以为我知道不同类型的Python引用的含义,但被这个示例弄糊涂了:

my_string = 'the cat sat on the mat' 
my_string = "the cat sat on the mat" 
my_string = """the cat sat on the mat""" 
my_string = '''the cat sat on the mat'''

前两种似乎是使用单引号或双引号声明字符串的两种不同方式。最后两个似乎是使表达式不完整的注释(并将在解释器中生成错误。是否正确?

最后两个是不是注释,它们是多行字符串,如果打印出最后两个字符串,您将看到在控制台上得到输出

my_string3 = """the cat sat on the mat"""

print my_string3
>>> the cat sat on the mat
如果
“…”
表示注释,则您必须在初始化变量时收到错误消息,如
a=#dad
将引发错误

由于使用
“”“
初始化的字符串是多行的,因此我们可以将一些字符串初始化为:

my_string3 = """the
cat sat on 
the mat""" 

print my_string3
>>> the
    cat sat on 
    the mat
示例:

text1 = "This is s single-line text with doubles"
text2 = 'This is s single-line text too but with singles'
text3 = "FallenAngel'S text should have doubles since there is an apostrophe in the string and signles would cause problems"

text4 = """This is a
        multi line string, and the output would be multi-line too"""
text5 = '''This is another multi-line.
        Both multi-lines generally used in writing Sql Query strings
        and this style makes them more readable'''
text6 = """Single line triple quotes are ok, but they do not look much useful"""
text7 = '''This is ok too, but as I said, it is not common and not pretty.'''

def somefunc():
    """And this is a doc-string. Python advice using triple double quotes in doc-strings even if they are single line"""
    pass

我想你会找到最好的答案@Scott:相同的链接对于“”和“”,都是相同的吗?
“…
”…“
都是相同的,因为它们都用于声明单行字符串,
“…”
'''.'''.'
都是用来声明多行注释的。从来没有见过这样的描述性单行和多行字符串,哈哈