Python - Different ways to comment code

By xngo on June 25, 2019

In Python, there are 2 ways to comment your code: single-line and multiple-line comment.

Single-line comment

A single-line comment starts with the pound(#) key and is automatically terminated by the end of line. For example,

# This whole line in itself is a comment.
 
pi = 3.14159 # This is the value of pi up to 5 decimal points.

Multiple-line comment

A multiple-line comment uses triple-single-quote(''') or triple-double-quote(""") as the delimiter. It starts with the delimiter and terminates with a line containing only the delimiter. For example,

""" This is a multiple lines comment that uses triple double quote.
It should end with a line containing only triple double quote.
"""
 
pi = 3.14159
 
''' This is another comment block that uses triple single quote.
It should end with a line containing only triple single quote.
'''

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.