Why are there two kinds of comments?


#1

Question

An earlier exercise showed comments using the # symbol, so why do we need another type of comment?

Answer

In Python, comments begin with a # symbol and continue to the end of the line. Text that is delimited by three double quotes at its beginning and end actually forms a multi-line string rather than a comment. In fact, a multi-line string is really just a string. Both comments and multi-line strings come in handy in different ways!

Single-line comments are great when you need to:

  1. Write a comment on the same line (inline), perhaps to explain a variable or value.

  2. Comment out a single line of code so that it doesn’t run.

Multi-line strings are useful when you need to:

  1. Remove large blocks of code from a process without deleting them.

  2. Write documentation for a program or function. If a string is positioned as the first line within a function, it serves as a docstring that can be accessed programmatically. For docstrings, it is conventional to use multi-line strings.

Ultimately the style and choice is up to you, but if you’re interested in going down the style guide rabbit hole, here’s a link to Python’s PEP 8 style guide!


FAQ: Learn Python - Python Syntax - Multi-line Strings