Formats

Python docstrings can be written following several formats as the other posts showed. However the default Sphinx docstring format was not mentioned and is based on reStructuredText (reST). You can get some information about the main formats in this blog post.

Note that the reST is recommended by the PEP 287

There follows the main used formats for docstrings.

- Epytext

Historically a javadoc like style was prevalent, so it was taken as a base for Epydoc (with the called Epytext format) to generate documentation.

Example:

"""
This is a javadoc style.

@param param1: this is a first param
@param param2: this is a second param
@return: this is a description of what is returned
@raise keyError: raises an exception
"""

- reST

Nowadays, the probably more prevalent format is the reStructuredText (reST) format that is used by Sphinx to generate documentation. Note: it is used by default in JetBrains PyCharm (type triple quotes after defining a method and hit enter). It is also used by default as output format in Pyment.

Example:

"""
This is a reST style.

:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""

- Google

Google has their own format that is often used. It also can be interpreted by Sphinx (ie. using Napoleon plugin).

Example:

"""
This is an example of Google style.

Args:
    param1: This is the first param.
    param2: This is a second param.

Returns:
    This is a description of what is returned.

Raises:
    KeyError: Raises an exception.
"""

Even more examples

- Numpydoc

Note that Numpy recommend to follow their own numpydoc based on Google format and usable by Sphinx.

"""
My numpydoc description of a kind
of very exhautive numpydoc format docstring.

Parameters
----------
first : array_like
    the 1st param name `first`
second :
    the 2nd param
third : {'value', 'other'}, optional
    the 3rd param, by default 'value'

Returns
-------
string
    a value in a string

Raises
------
KeyError
    when a key error
OtherError
    when an other error
"""

Converting/Generating

It is possible to use a tool like Pyment to automatically generate docstrings to a Python project not yet documented, or to convert existing docstrings (can be mixing several formats) from a format to an other one.

Note: The examples are taken from the Pyment documentation

Answer from daouzli on Stack Overflow
๐ŸŒ
Python
peps.python.org โ€บ pep-0257
PEP 257 โ€“ Docstring Conventions | peps.python.org
This PEP documents the semantics and conventions associated with Python docstrings.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Each method docstring documents parameters and return values. Proper indentation ensures readability and compatibility with tools like help(). Comments (#): Explain code but are ignored by Python at runtime.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ docstrings
Python Docstrings (With Examples)
In this tutorial, we will learn about Python docstrings. More specifically, we will learn how and why docstrings are used with the help of examples.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - See Python Docstrings. Learn about the different types of docstrings & various docstring formats like Sphinx, Numpy, and Pydoc with examples now.
๐ŸŒ
Readthedocs
sphinxcontrib-napoleon.readthedocs.io โ€บ en โ€บ latest โ€บ example_google.html
Example Google Style Python Docstrings โ€” napoleon 0.7 documentation
Attributes: msg (str): Human readable string describing the exception. code (int): Exception error code. """ def __init__(self, msg, code): self.msg = msg self.code = code class ExampleClass(object): """The summary line for a class docstring should fit on one line.
๐ŸŒ
Real Python
realpython.com โ€บ documenting-python-code
Documenting Python Code: A Complete Guide โ€“ Real Python
December 21, 2023 - Whether youโ€™re documenting a small script or a large project, whether youโ€™re a beginner or a seasoned Pythonista, this guide will cover everything you need to know. Weโ€™ve broken up this tutorial into four major sections: Why Documenting Your Code Is So Important: An introduction to documentation and its importance ยท Commenting vs Documenting Code: An overview of the major differences between commenting and documenting, as well as the appropriate times and ways to use commenting ยท Documenting Your Python Code Base Using Docstrings: A deep dive into docstrings for classes, class methods, functions, modules, packages, and scripts, as well as what should be found within each one
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ development โ€บ contributing_docstring.html
pandas docstring guide โ€” pandas 3.0.2 documentation
On rare occasions reST styles like bold text or italics will be used in docstrings, but is it common to have inline code, which is presented between backticks. The following are considered inline code: ... Python code, a module, function, built-in, type, literalโ€ฆ
Top answer
1 of 6
1389

Formats

Python docstrings can be written following several formats as the other posts showed. However the default Sphinx docstring format was not mentioned and is based on reStructuredText (reST). You can get some information about the main formats in this blog post.

Note that the reST is recommended by the PEP 287

There follows the main used formats for docstrings.

- Epytext

Historically a javadoc like style was prevalent, so it was taken as a base for Epydoc (with the called Epytext format) to generate documentation.

Example:

"""
This is a javadoc style.

@param param1: this is a first param
@param param2: this is a second param
@return: this is a description of what is returned
@raise keyError: raises an exception
"""

- reST

Nowadays, the probably more prevalent format is the reStructuredText (reST) format that is used by Sphinx to generate documentation. Note: it is used by default in JetBrains PyCharm (type triple quotes after defining a method and hit enter). It is also used by default as output format in Pyment.

Example:

"""
This is a reST style.

:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""

- Google

Google has their own format that is often used. It also can be interpreted by Sphinx (ie. using Napoleon plugin).

Example:

"""
This is an example of Google style.

Args:
    param1: This is the first param.
    param2: This is a second param.

Returns:
    This is a description of what is returned.

Raises:
    KeyError: Raises an exception.
"""

Even more examples

- Numpydoc

Note that Numpy recommend to follow their own numpydoc based on Google format and usable by Sphinx.

"""
My numpydoc description of a kind
of very exhautive numpydoc format docstring.

Parameters
----------
first : array_like
    the 1st param name `first`
second :
    the 2nd param
third : {'value', 'other'}, optional
    the 3rd param, by default 'value'

Returns
-------
string
    a value in a string

Raises
------
KeyError
    when a key error
OtherError
    when an other error
"""

Converting/Generating

It is possible to use a tool like Pyment to automatically generate docstrings to a Python project not yet documented, or to convert existing docstrings (can be mixing several formats) from a format to an other one.

Note: The examples are taken from the Pyment documentation

2 of 6
354

The Google style guide contains an excellent Python style guide. It includes conventions for readable docstring syntax that offers better guidance than PEP-257. For example:

def square_root(n):
    """Calculate the square root of a number.

    Args:
        n: the number to get the square root of.
    Returns:
        the square root of n.
    Raises:
        TypeError: if n is not a number.
        ValueError: if n is negative.

    """
    pass

I like to extend this to also include type information in the arguments, as described in this Sphinx documentation tutorial. For example:

def add_value(self, value):
    """Add a new value.

       Args:
           value (str): the value to add.
    """
    pass
Find elsewhere
๐ŸŒ
Python
python.org โ€บ dev โ€บ peps โ€บ pep-0257
PEP 257 -- Docstring Conventions | Python.org
May 29, 2001 - >>> print repr(foo.__doc__) '\n This is the second line of the docstring.\n ' >>> foo.__doc__.splitlines() ['', ' This is the second line of the docstring.', ' '] >>> trim(foo.__doc__) 'This is the second line of the docstring.' ... This document has been placed in the public domain. The "Specification" text comes mostly verbatim from PEP 8 by Guido van Rossum.
๐ŸŒ
Real Python
realpython.com โ€บ how-to-write-docstrings-in-python
How to Write Docstrings in Python โ€“ Real Python
June 19, 2025 - In this guide on how to write docstrings in Python, youโ€™ll learn about best practices, standard formats, and common pitfalls to avoid, ensuring your documentation is accessible to users and tools alike.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ docstrings
Python Docstrings: Syntax, Usage, and Examples
Python docstrings are special string literals used to document functions, classes, and modules. Unlike regular comments, docstrings are stored as metadata and can be accessed programmatically using built-in tools like the help() function. PEP 257 is a style guide focused on docstrings.
๐ŸŒ
Python-sprints
python-sprints.github.io โ€บ pandas โ€บ guide โ€บ pandas_docstring.html
pandas docstring guide โ€” Python documentation
Video tutorial: Pandas docstring guide by Frank Akogun. A Python docstring is a string used to document a Python module, class, function or method, so programmers can understand what it does without having to read the details of the implementation.
๐ŸŒ
Dataquest
dataquest.io โ€บ blog โ€บ documenting-in-python-with-docstrings
Tutorial: Documenting in Python with Docstrings
December 13, 2024 - Documenting your code is a critical skill for any data scientist or software engineer. This tutorial will show you how using docstrings.
๐ŸŒ
Zencoder
zencoder.ai โ€บ home โ€บ python docstring guide: format, examples & best practices
Python Docstring Guide: Format, Examples & Best Practices
December 3, 2025 - Proper use of docstrings improves ... documentation. This guide dives deep into Python docstring, covering formats, examples, best practices, and how to leverage them to write clean, professional Python code....
๐ŸŒ
Pandas
pandas.pydata.org โ€บ pandas-docs โ€บ stable โ€บ development โ€บ contributing_docstring.html
pandas docstring guide โ€” pandas 2.3.3 documentation
On rare occasions reST styles like bold text or italics will be used in docstrings, but is it common to have inline code, which is presented between backticks. The following are considered inline code: ... Python code, a module, function, built-in, type, literalโ€ฆ
๐ŸŒ
Zero To Mastery
zerotomastery.io โ€บ blog โ€บ python-docstring
Beginner's Guide to Python Docstrings (With Code Examples) | Zero To Mastery
September 27, 2024 - Struggling with code documentation? Learn how to write Python docstrings for better readability and maintainability, with automated documentation.
๐ŸŒ
Readthedocs
numpydoc.readthedocs.io โ€บ en โ€บ latest โ€บ format.html
Style guide โ€” numpydoc v1.11.0rc0.dev0 Manual
This document describes the syntax and best practices for docstrings used with the numpydoc extension for Sphinx. ... For an accompanying example, see example.py. Some features described in this document require a recent version of numpydoc. For example, the Yields section was added in numpydoc 0.6. We mostly follow the standard Python style conventions as described here:
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_docstrings.htm
Python - Docstrings
In Python, docstrings are a way of documenting modules, classes, functions, and methods. They are written within triple quotes (""" """) and can span multiple lines. Docstrings serve as convenient way of associating documentation with Python code.
๐ŸŒ
Google
google.github.io โ€บ styleguide โ€บ pyguide.html
Google Python Style Guide
Python uses docstrings to document code. A docstring is a string that is the first statement in a package, module, class or function. These strings can be extracted automatically through the __doc__ member of the object and are used by pydoc. (Try running pydoc on your module to see how it looks.)
๐ŸŒ
Idmod
docs.idmod.org โ€บ projects โ€บ doc-guidance โ€บ en โ€บ latest โ€บ docstrings.html
Google style Python docstrings โ€” Doc guidance documentation
The parameter list in Python docstrings is formatted very similar to a definition list. Remember to introduce code snippets with a double colon, indent the code sample, and include a blank line before and after the code.