What is comment in Python?
In Python, a comment is a piece of text in code that is not executed when the program is executed.
Comments are used to provide explanations, and keep documentation, or notes within the code for future reference and code understanding.
Python supports two types of comments: single-line comments and multi-line comments (docstrings).
You can use triple-quoted strings (single or double quotes) to create a multi-line comment in Python.
Anything between triple single-quotes (''' and ''') or triple double-quotes (""" and """) is treated as a docstring or a string literal, and it is ignored by the Python interpreter.
Creating multi-line comment using triple single-quotes
You can create a multi-line string literal using triple single-quotes ('''
). Any text between '''
and '''
will be treated as a multi-line comment.
In the following example, the text 'This is an example of a multi-line comment'
is placed between '''
and '''
to create a multi-line comment. The lines will not be treated as code by the Python interpreter.
Please take a closer look at the following example:
Example of Python Multi-line Comment
Output of the above example
Learning Python is a great fun!
The output does not contain the commented lines as they were ignored by the Python interpreter.
Creating multi-line comment using triple double-quotes
You can also create a multi-line string literal using triple double-quotes ("""
). Any text between """
and """
will be treated as multi-line comment.
In the following example, the text 'This is another example of a multi-line comment'
is placed between """
and """
to create a multi-line comment. The lines will not be treated as code by the Python interpreter.
Please take a closer look at the following example:
Example of Python Multi-line Comment
Output of the above example
Learning Python is a great fun!
The output does not contain the commented lines as they were ignored by the Python interpreter.