Last modified: Oct 28, 2026

Python Docstrings and Help

Python is known for its readability and simplicity. One way to maintain clean, understandable code is through proper documentation. Docstrings and the help() function play a key role in this process.

In this article, we'll explore what docstrings are, how to write them, and how to use the help() function to access documentation directly from your Python interpreter.

What Are Docstrings?

Docstrings (short for documentation strings) are string literals that appear as the first statement in a module, function, class, or method. They describe what the object does and how to use it.

Unlike regular comments, docstrings are accessible at runtime using the __doc__ attribute. They serve as built-in documentation for your code.

Why Use Docstrings?

Here are some reasons why docstrings are important:

  • They improve code readability.
  • They allow developers to generate external documentation automatically.
  • They make your code self-explanatory and easier to maintain.

How to Write Docstrings

Writing a docstring is simple. Just place a string literal right after the definition of a function, class, or module. Here's an example:


def greet(name):
    """
    This function greets the person passed in as a parameter.

    Parameters:
    name (str): The name of the person to greet.

    Returns:
    str: A greeting message.
    """
    return f"Hello, {name}!"

When you run this code, the docstring becomes part of the function object and can be accessed using greet.__doc__.

Accessing Docstrings

You can access docstrings using the __doc__ attribute. Let’s see how:


print(greet.__doc__)

    This function greets the person passed in as a parameter.

    Parameters:
    name (str): The name of the person to greet.

    Returns:
    str: A greeting message.

This makes it easy to retrieve documentation while coding or debugging.

Types of Docstrings

There are several common styles for writing docstrings. The most popular ones include:

1. Single-Line Docstrings

These are used for short functions or methods where the purpose is clear.


def square(x):
    """Return the square of x."""
    return x * x

2. Multi-Line Docstrings

These provide more detailed information and are ideal for complex functions or classes.


def add_numbers(a, b):
    """
    Add two numbers and return the result.

    Parameters:
    a (int or float): First number.
    b (int or float): Second number.

    Returns:
    int or float: Sum of a and b.
    """
    return a + b

3. Class Docstrings

Classes also benefit from docstrings. Here's an example:


class Dog:
    """
    A simple class representing a dog.

    Attributes:
    name (str): The name of the dog.
    age (int): The age of the dog.
    """

    def __init__(self, name, age):
        self.name = name
        self.age = age

The help() Function

The help() function is another powerful tool in Python. It invokes the built-in help system and displays documentation for any object.

To use it, simply pass the name of the function, module, or class without quotes:


help(greet)

Help on function greet in module __main__:

greet(name)
    This function greets the person passed in as a parameter.
    
    Parameters:
    name (str): The name of the person to greet.
    
    Returns:
    str: A greeting message.

This output includes the function signature and full docstring, making it very useful during development.

Getting Help on Built-in Functions

You can also get help for Python’s built-in functions. For instance:


help(len)

Help on built-in function len in module builtins:

len(obj, /)
    Return the number of items in a container.

This helps beginners understand how standard functions work without leaving the terminal.

Understanding advanced Python features can also enhance your knowledge. For example, learning about Python Keyword-Only Arguments Explained will help you write clearer function signatures.

Similarly, if you're curious about argument ordering, check out Python Positional-Only Arguments Explained.

Another modern feature is pattern matching. Discover how Python now supports switch-like behavior with Python Match Case: Simplified Switch Statement.

Best Practices for Writing Docstrings

Here are some tips to write effective docstrings:

  • Be concise but clear: Avoid unnecessary words.
  • Use proper grammar: Capitalize the first word and end with a period.
  • Include parameter descriptions: Especially for public APIs.
  • Document exceptions: Mention if the function raises any errors.

Example of a Well-Written Docstring


def divide(a, b):
    """
    Divide two numbers and return the result.

    Parameters:
    a (float): The numerator.
    b (float): The denominator.

    Returns:
    float: The quotient of a divided by b.

    Raises:
    ZeroDivisionError: If b is zero.
    """
    if b == 0:
        raise ZeroDivisionError("Cannot divide by zero.")
    return a / b

Conclusion

Docstrings and the help() function are essential tools for writing maintainable and readable Python code. They not only help others understand your code but also serve as a reference for yourself later.

By incorporating docstrings into your workflow and leveraging the help() function, you’ll find it easier to collaborate with other developers and build robust applications.

Start adding docstrings to your functions today and experience the difference in code clarity and usability.