pythoncomments

In Python, how are triple quotes (""") considered comments by the IDE?


My CS teacher told me that """ triple quotations are used as comments, yet I learned it as strings with line-breaks and indentations. This got me thinking - does python completely triple quote lines outside of relevant statements?

"""is this completely ignored like a comment"""

or, is the computer actually considering this?


Solution

  • Triple quoted strings are used as comment by many developers but it is actually not a comment, it is similar to regular strings in python but it allows the string to be in multi-line. You will find no official reference for triple quoted strings to be a comment.

    In python, there is only one type of comment that starts with hash # and can contain only a single line of text.

    According to PEP 257, it can however be used as a docstring, which is again not really a comment.

    def foo():
        """
        Developer friendly text for describing the purpose of function
        Some test cases used by different unit testing libraries
        """
        ...  # body of the function
    

    You can just assign them to a variable as you do with single quoted strings:

    x = """a multi-line text
    enclosed by
    triple quotes
    """
    

    Furthermore, if you try in repl, triple quoted strings get printed, had it really been a comment, should it have been printed?:

    >>> #comment
    >>> """triple quoted"""
    'triple quoted'