pythonpep8quotedocstringpep

Triple-double quote v.s. Double quote for docstrings


What is the preferred way to write Python doc string?

""" or "

In the book Dive Into Python, the author provides the following example:

def buildConnectionString(params):
    """Build a connection string from a dictionary of parameters.

    Returns string."""

In another chapter, the author provides another example:

def stripnulls(data):
    "strip whitespace and nulls"
    return data.replace("\00", "").strip()

Both syntax work. The only difference to me is that """ allows us to write multi-line doc.

Are there any differences other than that?


Solution

  • From the PEP8 Style Guide:

    PEP 257 recommends using triple quotes, even for one-line docstrings:

    Note that not even the Python standard library itself follows these recommendations consistently. For example,