How do I add multiline comments in Python?

Python does not have a built-in mechanism for multi-line comments. To comment out multiple lines in Python, you can comment out each line in a block by adding the "#" in front of each line. Many Python IDEs have shortcuts for such operations. A popular workaround for creating multi-line comments in Python is to use docstrings. If you use the docstring to comment multiple lines of code in Python, that block of code will be ignored and only lines outside the docstring will be executed. But this option is not recommended for frequent use because, unlike single-line comments, where the interpreter ignores the comment line, the docstrings are not deleted and take up system resources. Click Execute to run Python Multiline Comment Example online and see the result.

What is Python

Python is a high-level interpreted programming language used for developing websites, automating tasks, and analyzing data. The Python is a general-purpose language that can be used for various purposes without specialization in any particular field. Python has dynamic typing and garbage collection and supports several programming paradigms, including structured, object-oriented, and functional programming. The Python programming language runs on almost all operating systems such as Windows, Linux, and macOS. Python has built-in modules for working with HTTP requests, JSON, strings, and XML out of the box. Python is a straightforward programming language to learn and maintain.

What is a Python comment?

Comments in Python are lines of code that are ignored by the interpreter, but help developers describe and better understand how a particular piece of code works. Since a comment is a free text, programmers often comment out pieces of code to make it easier to debug and refactor programs, allowing them to see previous versions of the code. Commenting is important for all projects; it is an integral part of your workflow and is considered good practice for developers.

Python Comments Example
# This is a multiline comment
# in Python

print("I like Python")

# output: I like Python

How to use multiline lines docstring in Python?

Another way to describe how a particular piece of code works in multiline form is to use docstrings. A multiline docstring in Python starts with triple quotes (""") and ends with triple quotes ("""). The following is an example of how to use multi-line docstrings:

Python Multi-line Docstrings Example
"""
This is a multiline 
comment example
in Python
"""

print("I like Python")

# output: I like Python

See also