Python

Comments in Python

What is a Comment?

A comment is text written inside a Python program to provide information about the code.

Comments are not executed as Python instructions. They are mainly used to:

  • Explain what the code does
  • Make code easier to understand
  • Document important information

    Help other developers understand the program

    Temporarily prevent a line of code from executing during development

Python provides a simple way to write single-line comments using the # symbol.

For multiple lines, Python does not have a dedicated /* ... */ comment syntax. Triple-quoted strings (""" ... """ or ''' ... ''') are commonly used for multiline notes, although technically they are strings rather than comments.

1. Single-Line Comment

A single-line comment starts with the # symbol.

Everything written after # on that line is ignored by Python.

Example

# This is a comment

print("Hello World")

Output

Hello World

The comment is not displayed because Python does not execute it.

Comment After Code

You can also write a comment on the same line as your Python code.

name = "Ram"  # Store the student's name

print(name)

Output

Ram

Here:

name = "Ram"

is executed by Python, while:

# Store the student's name

is only a comment.

2. Multiple Single-Line Comments

You can create comments across multiple lines by using # on each line.

# This program calculates
# the total marks of a student
# and displays the result.

marks1 = 80
marks2 = 90
total = mar
ks1 + marks2

print(total)

Output

170

Each line beginning with # is a separate single-line comment.

This is the preferred way to write actual comments in Python when documenting code.

 

3. Multiline Comments

Python does not have a special multiline comment syntax such as:

/*
   This is a multiline comment
*/

which you may see in languages such as JavaScript, Java, or C++.

However, Python developers commonly use triple-quoted strings for writing multiline notes.

You can use either:

"""
This is a multiline text.
It can contain multiple lines.
"""

or:

'''
This is also
a multiline text.
'''

Example

"""
This program demonstrates
different Python data types.

It is created for
beginner-level practice.
"""

print("Python Data Types")

Output

Python Data Types

Important

Triple quotes create a multiline string, not technically a comment.

If the string is not assigned to a variable and is not used as a docstring, Python generally does nothing visible with it.

For normal code comments, using # is the recommended approach.

4. Triple Quotes as Multiline Text

Triple quotes are actually used to create strings that can span multiple lines.

Example

message = """
Hello Student,

Welcome to Python programming.

Keep practicing!
"""

print(message)

Output

Hello Student,

Welcome to Python programming.

Keep practicing!

In this example, the triple-quoted content is stored in the message variable, so it is a real string.

5. Docstrings

Triple-quoted strings have an especially important use in Python: documentation strings, commonly called docstrings.

A docstring describes what a module, class, or function does.

Example

def greet():
    """
    This function displays
    a welcome message.
    """
    print("Welcome to Python")


greet()

Output

Welcome to Python

The string immediately inside the function is the function's docstring.

You can access it using __doc__.

def greet():
    """
    Display a welcome message.
    """
    print("Welcome to Python")


print(greet.__doc__)

Output

    Display a welcome message.

Docstrings are important when creating professional Python applications, libraries, and APIs.

6. Single-Line vs Multiline Comments

TypeSyntaxPurpose
Single-line comment# commentExplain one line or a small piece of code
Multiple comment lines# on each lineWrite comments across several lines
Multiline string""" ... """Create text spanning multiple lines
Multiline string''' ... '''Alternative multiline string syntax
Docstring""" ... """ inside function/class/moduleDocument Python code
Interactive Sandbox
Python