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.


import_contacts

You may also like:

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • Return vs Print in Python
    16 April

    Return vs Print in Python

    Table of ContentsReturn vs Print in PythonConclusion Return vs Print in Python In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is […]

  • Floor division in Python
    10 April

    Floor division in Python

    Table of ContentsWhat is Floor Division in Python?How to implement floor division in Python?Using the // operator to implement floor division in Python.Using the // operator to implement floor division on negative integers in Python.Using the // operator to implement floor division on floating-point numbers in Python.Using the // operator to implement floor division with […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.