How to comment out block of code in Python?

If you are a Python developer, this article will help you understand “How to comment out block of code in Python?” and the types of comments in any programming language with example.

What are the comments in a programming language?

If you are a programmer or have knowledge of any programming language, then you must be aware of the importance of comments in the programming. A portion of the code with the help of comments that can span across multiple lines or part of the single line or an entire line can be skipped by the compiler while compiling and executing the code.

Using comments, you can also enter information related to function or part of code or program block in the code. This improves readability of code.

What is the difference between line comment and block comment?

Here we will take an example of a sample Java program to understand difference between line comment and block comment in programming language.

1. Line comment:

As shown below, you can use two forward slashes’ // at the start of the line that you want to comment out. This commented line will be skipped while compiling and executing the code by the compiler.

Comment a single line of code

Output of above program will be-

Hello Friends
Welcome to TechSupportWhale

2. Block comment:

If you want to comment out multiple lines or block of code, then you can add forward slashes // as explained above, in front of each line of the block code. But if the number of lines is more, then commenting each line will be time-consuming and frustrating. In this situation, you can use block comment to comment out a portion of the code.

Here is an example to comment out a block of code by adding /* at beginning of the first line and add */ at end of the last line.

Comment block of code (multiple lines)

Output of above program will be-

Welcome to TechSupportWhale

Read also: echo a new line in batch file

How to add line comment and block comment in python?

Line comment in Python:

As shown below, to comment line of code in python add #(hash) at the beginning of the line

Line comment in Python

Output of above program will be

Hello Friends
Welcome To TechSupportWhale

Block comment in Python:

Python does not support block comments. You cannot comment out a block of the code in Python. If you want to comment out multiple lines of code, then you have to add #(hash) at the start of each line of the code. Yes, this is the common and only way to comment out a block of code in Python that most of the developers know.

Block comment in Python

Some editors give option or shortcut keys to add #(hash) at the start of the selected lines. These options or shortcut keys varies based on the IDE you use.

Read also: Everything about conio.h library functions in C/C++

Alternate way to comment out block of the code in python:

Are you wondering if there is any alternate way to achieve block comments in Python? The answer is yes.

Using multiline-strings, you can comment out a block of the code in Python. Multiline-strings don’t generate any source code. So code inside multiline-string is considered as a string and will be skipped while executing the code.

You can add “”” at the beginning and at end of the block of code to comment multiple lines of code easily.

Below is an example to understand the use of multiline string for commenting.

Comment out block of code in Python

Note: Though using multiline-strings you can add block comment in python but technically this is not a correct way of coding. Python programmers we know have faced problems while using multiline-string to comment out block of code in Python. As a good practice, we don’t recommend this approach.

Conclusion:

We hope this article was informative and you got a clear idea about block comments in python. To summarize, use #(hash) instead of, multiline-string to comment out block of code in python.

If you have any other way you use as a best practice, do share your experience in comments section to help other fellow developers.

Cheers!!!

Leave a Comment

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

Scroll to Top