Python Comments and Docstrings

Python 5 min min read Updated: Mar 09, 2026 Beginner
Python Comments and Docstrings
Beginner Topic 9 of 10

Comments & Docstrings in Python

Comments and docstrings are used in Python to explain code and make programs easier to understand. Writing clear documentation inside the code helps other developers, as well as your future self, understand what a program is doing.

Comments are ignored by the Python interpreter during execution, while docstrings are used to document modules, functions, classes, and methods.

What are Comments in Python?

A comment is a line of text that explains the code. Comments are not executed by Python and are only used to improve code readability.

In Python, comments start with the # symbol.

python # This is a comment print("Hello Python")

The interpreter ignores the comment and only executes the print statement.

Single-Line Comments

Single-line comments are commonly used to explain a specific line of code.

python # Store user age age = 25 # Display the age print(age)

These comments help programmers understand the purpose of each line.

Inline Comments

Inline comments are written on the same line as the code.

python score = 90 # Student exam score print(score)

Inline comments should be used carefully so they do not reduce readability.

Multi-Line Comments

Python does not have a dedicated syntax for multi-line comments, but multiple single-line comments can be used together.

python # This program calculates # the average of numbers # entered by the user

Another common method is to use triple quotes, which are technically multi-line strings but often used as comments.

python """ This program demonstrates multi-line documentation in Python. """

What are Docstrings?

Docstrings (documentation strings) are special strings used to document Python modules, functions, classes, and methods. They are written using triple quotes (''' or """).

Docstrings are different from comments because they are stored as part of the object and can be accessed using Python's built-in documentation tools.

Function Docstring Example

python def add_numbers(a, b): """ This function takes two numbers and returns their sum. """ return a + b print(add_numbers(5, 3))

The text inside triple quotes describes what the function does.

Accessing Docstrings

You can access a docstring using the __doc__ attribute.

python def greet(name): """Returns a greeting message.""" return "Hello " + name print(greet.__doc__)

This prints the documentation of the function.

Docstrings for Classes

Docstrings can also describe classes and their behavior.

python class Student: """Represents a student with name and age.""" def __init__(self, name, age): self.name = name self.age = age

This docstring explains the purpose of the class.

Types of Docstrings

  • Module Docstrings – Describe the purpose of an entire module.
  • Function Docstrings – Explain what a function does.
  • Class Docstrings – Document classes and their attributes.
  • Method Docstrings – Explain methods inside a class.

Best Practices for Writing Comments and Docstrings

  • Write comments only when necessary.
  • Keep comments clear and concise.
  • Use docstrings to document functions and classes.
  • Follow Python documentation conventions such as PEP 257.
  • Avoid redundant comments that simply repeat the code.

Real-World Example

python def calculate_area(radius): """ Calculate the area of a circle. Parameters: radius (float): radius of the circle Returns: float: area of the circle """ pi = 3.14159 return pi * radius * radius

This docstring clearly explains the purpose of the function and its parameters.

Conclusion

Comments and docstrings are important for writing clean, maintainable Python code. Comments help explain specific lines of code, while docstrings provide detailed documentation for functions, classes, and modules.

Using proper documentation makes code easier to understand, debug, and maintain, especially when working in teams or building large applications.

In the next tutorial, we will explore Functions in Python and learn how to create reusable blocks of code.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators