pythonsyntax

What does ''' mean in Python?


Have been told that in Python

'''

is used to indicate the start of a multi-line string. However I have also been taught that this code also allows for the documentation of functions and modules.

Googling, surprisingly, doesn't give a clear answer on what ''' definitively refers to.

So how should I remember, as a beginner, what this Python code refers to? A multi-line string? An operator to assist documentation? Both? Something else?


Solution

  • Triple quotes ''' (and """) is a marker for a string literal just like quote characters ' and ", which you can see in Python's grammar for String and Bytes literals.

    Its only difference to regular quotes is that newlines and unescaped quote characters are allowed within a triple-quoted string literal, which makes triple quotes ideal for documentation in natural language where newlines and unescaped quote characters can appear often.

    It is why as a convention triple quotes are used for docstrings, as suggested in PEP-257, although you can still use regular quotes for docstrings.