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
🌐
PyPI
pypi.org › project › pydocstringformatter
pydocstringformatter · PyPI
A tool to automatically format Python docstrings that tries to follow recommendations from PEP 8 and PEP 257.
🌐
GitHub
github.com › PyCQA › docformatter
GitHub - PyCQA/docformatter: Formats docstrings to follow PEP 257 · GitHub
docformatter formats docstrings compatible with black when passed the --black option. docformatter formats field lists that use Epytext or Sphinx styles. See the the full documentation at read-the-docs, especially the requirements section for a more detailed discussion of PEP 257 and other requirements. ... With Python >=3.11, tomllib from the standard library is used.
Author: PyCQA
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
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python Docstring Formats (Styles) and Examples | note.nkmk.me
August 26, 2023 - sphinx.ext.napoleon – Support for NumPy and Google style docstrings — Sphinx documentation · Some IDEs and editors provide type hints and error checking for these styles. For example, PyCharm, an integrated development environment for Python, supports all three styles.
🌐
GitHub
github.com › DanielNoord › pydocstringformatter
GitHub - DanielNoord/pydocstringformatter: Automatically format your Python docstrings to conform with PEP 8 and PEP 257 · GitHub
A tool to automatically format Python docstrings to follow recommendations from PEP 8 and PEP 257 (or other supported style guides.) See What it does for currently supported auto-formatting.
Starred by 87 users
Forked by 8 users
Languages: Python
🌐
DataCamp
datacamp.com › tutorial › docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Find different examples & format types of docstrings for Sphinx, Numpy and Pydoc. ... If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course.
🌐
Google
google.github.io › styleguide › pyguide.html
Google Style Guides | Style guides for Google-originated open-source projects
The presence of a trailing comma ... to our Python code auto-formatter Black or Pyink to direct it to auto-format the container of items to one item per line when the , after the final element is present. Yes: golomb3 = [0, 1, 3] golomb4 = [ 0, 1, 4, 6, ] ... Two blank lines between top-level definitions, be they function or class definitions. One blank line between method definitions and between the docstring of a class ...
Find elsewhere
🌐
Tessl
tessl.io › registry › tessl › pypi-docformatter › 1.7.0
1.7.0 • pypi-docformatter • tessl • Registry • Tessl
June 4, 2026 - Formats docstrings to follow PEP 257 conventions with support for various docstring styles and Black formatter compatibility ... A comprehensive Python docstring formatter that automatically formats docstrings to follow PEP 257 conventions.
🌐
Real Python
realpython.com › how-to-write-docstrings-in-python
How to Write Docstrings in Python – Real Python
August 25, 2025 - Make sure you’re in the same directory as magical_characters.py and run the following command to display the formatted documentation: ... $ python -m pydoc magical_characters Help on module magical_characters: NAME magical_characters - A module for adding and listing magical characters. FUNCTIONS add_characters(magical_being) Add a new magical character. FILE /Users/rp/projects/python-docstrings/magical_characters.py
🌐
Python
peps.python.org › pep-0287
PEP 287 – reStructuredText Docstring Format | peps.python.org
When plaintext hasn’t been expressive enough for inline documentation, Python programmers have sought out a format for docstrings. This PEP proposes that the reStructuredText markup be adopted as a standard markup format for structured plaintext documentation in Python docstrings, and for PEPs and ancillary documents as well.
🌐
GitHub
github.com › iansan5653 › vscode-format-python-docstrings
GitHub - iansan5653/vscode-format-python-docstrings: VSCode formatter extension implementation of docformatter. · GitHub
June 16, 2021 - If you already have a Python formatting ... file. In a Python file in VSCode, ctrl+shift+p to open the command pallette, run the command Format Document With... and select Python Docstring Formatter....
Author: iansan5653
🌐
Real Python
realpython.com › videos › write-format-your-docstrings
Writing and Formatting Your Docstrings (Video) – Real Python
Write and Format Your Docstrings. The mkdocstrings package can pull valuable information from your codebase to help auto-generate parts of your documentation. As the package name suggests, you’ll need docstrings to accomplish this. It can also make…
Published: September 20, 2022
🌐
Astral
docs.astral.sh › ruff › formatter
The Ruff Formatter - Astral Docs
For more on configuring Ruff via pyproject.toml, see Configuring Ruff. Given the focus on Black compatibility (and unlike formatters like YAPF), Ruff does not currently expose any other configuration options. The Ruff formatter provides an opt-in feature for automatically formatting Python ...
🌐
Ubuntu
manpages.ubuntu.com › jammy › man(1)
Ubuntu Manpage: docformatter - Formats docstrings to follow Python PEP 257 (Python 3)
Formats docstrings to follow PEP 257. files · files to format or '-' for standard in · -h, --help · show this help message and exit · -i, --in-place · make changes to files instead of printing diffs · -c, --check · only check and report incorrectly formatted files ·
🌐
Josh Di Mella
joshdimella.com › blog › python-docstring-formats-best-practices
A Guide to Python Docstring Formats: Choosing the Right Style for Your Code | Josh Di Mella | Software Engineer
May 31, 2023 - In this blog post, we'll explore different Python function docstring formats to help you choose the right style for your codebase. In this section, we will explore various Python function docstring formats commonly used in the community. We will provide examples for each format, including Google style docstrings, Sphinx/reStructuredText style docstrings, NumPy style docstrings, and Epytext style docstrings.
🌐
Reddit
reddit.com › r/neovim › auto formatting of python docstrings
r/neovim on Reddit: Auto formatting of python docstrings
September 6, 2024 -

I'm using black as my formatter for python, but it doesn't touch docstrings at all. For example, even though I have set up a 79 character limit for lines, docstrings are not automatically broken to fit the limit. Spacing in docstrings also isn't automatically set, and neither are indentation, etc.

(I also use Neogen for docstring creation but that does nothing for formatting)

Is there a way to achieve such automation? Would be happy for recommendations.

🌐
Stack Abuse
stackabuse.com › common-docstring-formats-in-python
Common Docstring Formats in Python
August 26, 2023 - These formats not only help in understanding the code, but they also allow tools like Sphinx, PyDoc, and Doxygen to automatically generate well-formatted documentation. We'll look at these formats in the following sections. Docstrings in Python are a powerful tool for documenting your code.