How To Comment Out In Python?

Comments are very useful to explain Python code. The comment out means converting existing Python statements and code into a comment by using single line or multiline comment. Comment out is very useful to create testing statements and disabling them by commenting out. For example, you may add some print() statement to print variable values and then comments out after debugging values for later use.

Comment Out Single Line Comment

The most popular comment type is single line comment . As expected the single line comment consists of a single line of comment. This is the most popular comment-out method where a single line of Python statement can comment out by using the hash mark. Before comment out the Python code is like below.

a = 35

print(a)

result = a *2

In this Python code, we comment out the print(a) statement which is normally used to print the variable a value for debugging. We just put a hash mark or comment sign at the start of the print(a) line like below.

a = 35

# print(a)

result = a *2

Comment Out Single Line Double or Single Quote Comment

Single or double quote can be used to comment out. Even this is not the native and suggested way they can be used for comment out. But keep in mind that comment out single or double quote do not works properly and creates errors if the commented out statements or codes contains single or double quote string.

a = 35

print(a)

result = a *2

We comment out the print(a) statement with single quote like below.

a = 35

'print(a)'

result = a *2

Alternatively the double quote can be used to comment out like below.

a = 35

"print(a)"

result = a *2

Comment Out Multiline/Block Comment

In some cases, we may need to comment out multiline or block Python statements with the multiline/block comments. This is the same with single-line comment out where every line we want to comment out started with the hash mark or comment sign.

a = 35

print(a)
square_root = a*a

result = a *2

We comment out multiline and block Python code print(a) and square_root=a*a with the hash mark like below.

a = 35

#print(a)
#square_root = a*a

result = a *2

Leave a Comment