Python comments are programmer-readable explanations or annotations in the Python source code. They are added with the purpose of making the source code easier for humans to understand and are ignored by the Python interpreter. Comments enhance the readability of the code and help the programmers understand the code very carefully.

Just like most modern languages, Python supports single-line (or end-of-line) and multi-line (block) comments. Python comments are very similar to the comments available in PHP, BASH, and Perl programming languages.

There are three types of comments available in Python

  • Single line Comments
  • Multiline Comments
  • Docstring Comments
  • Single Line Comments

A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment, and the Python interpreter ignores them.


Following is an example of a single-line comment in Python:

# This is a single line comment in python
print ("Hello, World!")

This produces the following result −

Hello, World!

You can type a comment on the same line after a statement or expression −

name = "Madisetti" # This is again comment

Multi-Line Comments

Python does not provide a direct way to comment on multiple lines. You can comment multiple lines as follows −

# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

The following triple-quoted string is also ignored by the Python interpreter and can be used as a multiline comment:

This is a multiline

Following is an example showing the usage of multi-line comments:

This is a multiline
print ("Hello, World!")

This produces the following result −

Hello, World!

Docstring Comments

Python docstrings provide a convenient way to provide help documentation with Python modules, functions, classes, and methods. The docstring is then made available via the __doc__ attribute.

def add(a, b):
    """Function to add the value of a and b"""
    return a+b

This produces the following result −

Function to add the value of a and b

Related Articles and Resources

Python Variables

A variable is a name that is used to refer to a memory location. Variables in Python are used to store data and are also …

How To Install Python

In order to become Python developer, the first step is to learn how to install or update Python on a local machine or computer. In …

Python Features

Python differs from other programming languages in terms of popularity and value because it offers a number of practical features. It supports procedural and object-oriented …

Python - Basic Syntax

The Python syntax defines a set of rules that are used to create Python statements while writing a Python program. The Python Programming Language Syntax …

Plot Correlation Matrix In Python

Given two variables, if the value of one variable is dependent on the value of the other variables, we say the variables are related. The …

Python Literals

Python Literals can be defined as data that is given in a variable or constant.Python supports the following literals:String literals:String literals can be formed by …

Python Data Types

Python data types are used to specify a variable's type. It specifies the kind of information that will be kept in a variable. Different kinds …

Python Tutorial

This Python tutorial explains both basic and advanced Python concepts. This Python tutorial is intended for both beginners and experts. You'll have a strong foundation …

Trusted by digital leaders and practitioners from 100+ International Organizations