Python Comments

In this lesson, We will learn about Python comments. Also, we will learn about the different types of Python comments and how to use them.

What is a comment in Python?

Comments are marked lines that make a code easier to comprehend and self-explanatory. The Python interpreter ignores all comments in the code. There are several methods to produce a comment depending on the type we want to add to our code.

The several comment types can be used in our Python program as follows.

  • Single Line Comments
  • Multiline Comments

Let’s talk about each of the remarks mentioned above kinds individually.

Single Line Comments

The # character denotes python comments on single lines. All characters beginning with the # character (and continuing till the end of the line) are considered part of the comment.


#Single-line comment
print ("Hello! World")
print ("Hello! World")#Single-line comment


Hello! World

Here, the comment is a Single-line comment.

The Python interpreter ignores this line, and the characters after the # are not used. Thus, you can write the above program in a single line too.

Multiline Comments

Unlike certain programming languages like C, Java, and others, Python does not have a dedicated capability for multiline comments. However, it does not follow that making multiline comments in Python is completely impossible.

There are two ways we can add comments to our Python code that span many lines. As shown below, you can comment on many lines:

  1. Using Multiple Single-Line Comments (Hash Sign #)
  2. String Literals for Multi-line Comments

Using Multiple Single-Line Comments (Hash Sign #)

We can utilize multiple single-line comments for a whole block. Each line of a comment on multiple lines can start with #, and a multiline comment is created using several # signs.


#Python Block
#A multiline comment
#By implementing multi hash sign

In this case, every line is treated as a single comment and is ignored.

docstrings for Python

docstrings are the triple quotes that follow the definition of a function, method, or class (documentation strings). Docstrings are denoted by closing and opening quotations.
With the help of the __doc__ attribute, docstrings can be read and linked to objects. Visit Python docstrings to learn more.

String Literals for Multi-line Comments

The string literals can be used as a multiline comment and are also ignored by the Python interpreter. Many programmers utilize them as multiline comments in Python. Although it is OK to utilize docstrings to create multiline comments, it is crucial to remember that docstrings and comments serve quite different purposes. While docstrings, when used inside the Python function, can be viewed at run time, the Python Interpreter completely disregards comments in Python.
So, we can write a single-line comment using a string literal below


‘It is also a comment using String Literals’
Similar to multiline comments, you can use multiline strings (triple quotes) to create comments. Here, we can see that the program’s statement, which is a string, is not assigned to a variable or function. Thus, the string is ignored by the interpreter.

Either’or “may be used as the quote mark.


multiline comment!
Hello World
multiline comment!
print("Hello World")


Hello World

The interpreter will not print all the statements in the above example enclosed by triple quotes. The interpreter ignores the multiline string because it isn’t set to a variable in this case and can be used as a multiline comment even though it isn’t officially one.

What’s good about comments?

  • Adding comments to our code makes it easier to understand. It makes the code easier to understand and helps us remember why we wrote certain parts of code.
  • Aside from that, you can also use comments to ignore some code while testing other blocks, making it easy to stop some lines from running or write a quick pseudo-code for the program.

Tips for Writing Good Comments

  • Instead of going into specifics about how a function works, use comments to describe what it does.
  • Remove as many unnecessary remarks as you can. Consider implementing better function and variable naming in your code so it can be self-explain.
  • Keep your remarks brief and to the point as much as you can.