How to comment out multiple lines in python

How to comment out multiple lines in python

In this article, we will see about how to comment out multiple lines in python.

The concept of comments is present in most programming languages. We use comments for documentation purposes. The compiler ignores them, but the user can use comments to describe what is happening in the code.

As the code grows longer and becomes more complicated, comments prove to be a good way of explaining the code.

In Python, we use the # character to initiate a comment. We use this for declaring single-line comments.

Most programming languages support multi-line comments also. However, in Python, we can only have one-line comments. We cannot directly comment out multiple lines as comments in Python.

Multiple Line Comments in Python

We will discuss how to emulate such multiple-line comments in this article.

Using the # character

To have multiple line comments, we can declare a single-line comment in every line. This method is tedious but works nevertheless.

For example,

Using triple quotes

Using triple quotes, we can create multi-line strings in Python. It is a good way to emulate multi-line comments, but one must be careful about the indentation.

For example,

In the above example, we can see that such strings successfully emulate multi-line comments. However, if one is not careful, they might turn into accidental docstrings.

A docstring conveys the working and description of a function, module, or any other object. We declare them right after defining the function or at the start of the module.

However, we can also hide this string in context and prevent them from being mistaken as docstrings.

See the code below.

Let us understand the above code. The if False: statement will never execute, so we hide the docstring in plain sight.

Using IDEs and Text Editors

Some IDEs and editors allow us to comment out blocks. On JetBrains PyCharm, we can select the code block and use control + / key to comment it. Similarly, the combination of control + k can comment out a code block in Python Tools for Visual Studio.

That’s all about how to comment out multiple lines in python.

Related Posts

  • Python check for null
    17 November

    How to check for null in Python

    Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

  • Sort dictionary by value in Python
    17 November

    Sort dictionary by value in python

    Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

  • Get class name in Python
    17 November

    Get class name in Python

    Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

  • Python print to file
    17 November

    How to Print to File in Python?

    Table of ContentsThe print() function in pythonHow to Print to file in python?Print to file using the file parameter in the print functionPrint to file by redirecting standard outputConclusion We use the print() function in python to print any text to standard output. In this article, we will see how we can use the print() […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.