Writing Comments in Python

For single line comments, you may use the # (hash character) in the Python programs.

For multi-line comments, use the triple quoted strings.

Both of these ways are explained below with examples.

Python Single line comment example code

#This is how you write single line comment in Python using hash symbol

A multiline comment code example

'''For writing multiline comment, just use

Triple single quotes

One before the text and the other at the last

'''

A brief Intro about commenting

  • Writing comments is a very important aspect of any programming language.
  • The comments enable programmers writing things that are important to the context of code.
  • Similarly, multiline comments may be required to write the purpose of code on top of the program along with author information.
  • Python parser ignores the comments within the program.
Related reading: Python pass statement

For example, you have a nested loop that goes to three levels. Before starting the outer loop, you may write the purpose of all loops.

After some time, this is natural to forget even your own code or you have to remember things yourself. If you look back after a year or so, you may understand the purpose by reading those comments.

Similarly, if you have written a class or functions that are usable by other programmers.

This will be helpful for them to read some guidelines within the code if they are required to modify or maintain things in the future.

Writing single line comment example

You may write the single line comments by the hash character (#). Just start a string with a hash sign and write a comment in a single line as shown in the example below:

#An example of commenting in Python – single line



print ("How to write comments in Python?") #May also write comment ahead of code

print ("has character # in a string is not comment") #in a string is a part of string

        #comment after a few spaces also works

Python comment

A few things to be noted in above example regarding Python comments:

  • You may comment on top of the program using # character.
  • The comment can also be written ahead of a line of code – inline comments.
  • The comment will not produce an error if written after whitespaces.
  • If your string objects contain a hash character, it will not be treated as comment but part of the string.

An example of multiline comments

By using # character, you may write a single line comment that goes to the end of the physical line. You have to add # for each new line.

For writing multiline comments, you may use a triple quoted string.

Add the triple quoted string at the beginning and end of a comment as used in the example below:

'''The program uses a list of years

The list is used with filter function along with

lambda to calculate leap years

'''

Years_List = [2000, 2002, 2006, 2008, 2011, 2012, 2016]



#Leap_Years = list(filter(lambda leap_yrs: (leap_yrs%4 == 0) , Yr_List))



print("Leap years in List " ,list(filter(lambda leap_yrs: (leap_yrs%4 == 0) , Years_List)))

multiline comment

Unless they are used as docstrings, they generate no code.

Recommendation for block or multiline commenting

The official document recommends using consecutive # characters for block commenting. So, despite using the triple-quoted comment:

'''

Multiline commenting way

In Python

'''

You should use:

# Multiline commenting way

# in Python

And if you have paragraphs, then use only a hash character in the new line as follows:

# Multiline commenting way

#

# in Python

Short-key for commenting using IDLE

If you are using the IDLE editor for developing Python programs, you may use the short-keys or menu options.

For commenting a line, bring the cursor to the line that you want to comment and press Alt+4.

To uncomment an existing comment, press Alt+3.

comment short key

The comment/uncomment options are available under the Format menu. Use “Comment Out Region” / “Uncomment Region” for comment and uncomment the line, respectively.It adds two ## characters at the beginning.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!