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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b9c14c26573018096505/] [crayon-63b9c14c2657a070015690/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63b9c14c2674d705774721/] [crayon-63b9c14c26753606949291/] To call a function, […]

  • 27 December

    [Fixed] NameError Name ‘unicode’ is Not Defined in Python

    Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63b9c14c2692b341233912/] [crayon-63b9c14c26931294819708/] Unicode is a computing industry standard that ensures that text from most of […]

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

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.