Hit enter after type your search item

Writing Python comments

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.

See quick example with commenting:

# comment example Python multiline comment example

Python Single line comment example code

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

A multiline comment code example


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 etc.

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 remind 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 require 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:

See online demo and code


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 physical line. You have to add # for each new line.

For writing multiline comments, you may use triple quoted string. Add the triple quoted string at the beginning and end of a comment as used in the demo below:

See online demo and code


multiline comment

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

Recommendation for block or multiline commenting

The official document recommends using the consecutive # characters for block commenting. So, in spite of using the triple quoted comment:


You should use:

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

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.

This div height required for enabling the sticky sidebar