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
The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated.
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
Discussions

How do YOU format docstrings
647k members in the learnpython community. Subreddit for posting questions and asking for general advice about your python … More on reddit.com
🌐 r/learnpython
July 4, 2017
How should I specify the docstring format?

a) Not quite. See www.python.org/dev/peps/pep-0008/ (PEP 8)

b) No. Not standard.

More on reddit.com
🌐 r/Python
4
12
March 27, 2012
What's the best guide/model to writing good docstrings for modules/classes/methods?
Check out this stack overflow answer . I won't copy paste the good info from that, but I would highlight this: Note that the reST is recommended by the PEP 287 And... Nowadays, the probably more prevalent format is the reStructuredText (reST) format In my experience, this is the case. More on reddit.com
🌐 r/learnpython
3
14
August 27, 2017
A case for better Python docstrings

The varname::type syntax seems a bit unnecessary when Python 3 already has type hints/annotations in the form of PEP 484 and PEP 526. The existing PEP syntax also has the advantage of being understood by current IDEs and tools such as pylint.

I guess this would be useful for Python 2 as it doesn't have type hints, but the :: symbol still looks kind of foreign in Python.

More on reddit.com
🌐 r/programming
13
27
August 26, 2018
🌐
DataCamp
datacamp.com › tutorial › docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Docstrings are string literals ... documentation for Python modules, classes, and methods, and are typically written in a specialized syntax called "reStructuredText" that is used to create formatted documentation....
🌐
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 - Google style docstrings follow a structured format and are widely adopted in the Python community. They consist of a summary line, detailed explanations, and specific sections for parameters, return values, and raised exceptions. This format ensures comprehensive and standardized documentation, ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Example 2: This function shows how to use triple double quotes for docstrings. ... def my_func(): """This is a docstring using triple double quotes.""" return None print(my_func.__doc__) ... This is a docstring using triple double quotes. Google style docstrings follow a specific format and are inspired by Google's documentation style guide. They provide a structured way to document Python code, including parameters, return values and descriptions.
🌐
Real Python
realpython.com › how-to-write-docstrings-in-python
How to Write Docstrings in Python – Real Python
August 25, 2025 - In this format, Args lists parameters and their descriptions, Returns describes the return value and its type, and Raises (when included) shows exceptions that might be raised by the function. Google-style docstrings shine when you need a detailed, consistent structure—especially if you’re collaborating on large projects or using documentation generators like Sphinx. The NumPy style of docstrings is favored in scientific and data-oriented Python projects.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python Docstring Formats (Styles) and Examples | note.nkmk.me
August 26, 2023 - In Python, strings written at the beginning of definitions such as functions and classes are treated as docstrings (documentation strings). IDEs or editors may offer keyboard shortcuts to display docs ...
Find elsewhere
🌐
Programiz
programiz.com › python-programming › docstrings
Python Docstrings (With Examples)
Python docstrings are the string literals that appear right after the definition of a function, method, class, or module.
🌐
Dataquest
dataquest.io › home › blog › how to use python docstrings for effective code documentation
Tutorial: Documenting in Python with Docstrings
December 13, 2024 - PEP 257 summarizes Python docstrings. There are four primary docstring formats: NumPy/SciPy docstrings, Google docstrings, reStructuredText, and Epytext. The first two are the most common.
🌐
AskPython
askpython.com › python › python-docstring
Python Docstring - AskPython
February 16, 2023 - If a class method is overriding ... argument names exactly the same as in the function definition. There are no rules associated with the format of the docstring....
🌐
FavTutor
favtutor.com › blogs › docstring-python
Python Docstring: How to Write Docstrings? (with Examples)
June 6, 2023 - A docstring has a straightforward format: it is a string surrounded by triple quotes that appear as the first statement of a function, method, or module. Here is an illustration of a Python function's documentation string:
🌐
Noirlab
datalab.noirlab.edu › docs › manual › DevGuide › DocumentingPythonAPIswithDocstrings › DocumentingPythonAPIswithDocstrings.html
3.2. Documenting Python APIs with Docstrings — Data Lab documentation
We organize Python docstrings into sections that appear in a common order. This format follows the Numpydoc standard (used by NumPy, SciPy, and Astropy, among other scientific Python packages) rather than the format described in PEP 287. These are the sections and their relative order: ... For functions and methods, write in the imperative voice.
🌐
Stack Abuse
stackabuse.com › common-docstring-formats-in-python
Common Docstring Formats in Python
August 26, 2023 - In this article, we'll delve into what a docstring is and explore some of the most common docstring formats used in Python. A docstring, short for documentation string, is a literal string used right after the definition of a function, method, class, or module.
🌐
Alvinntnu
alvinntnu.github.io › python-notes › python-basics › docstrings.html
Docstrings Format — Python Notes for Linguistics
""" 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. If the class has public attributes, they may be documented here in an ``Attributes`` section and follow the same formatting as a function's ``Args`` section.
🌐
Mimo
mimo.org › glossary › python › docstrings
Python Docstrings: Syntax, Usage, and Examples
It suggests keeping a summary line on the first line of your docstring, followed by a blank line (for longer docstrings), and then more details if needed. The summary line should briefly state what the function, class, or module does. ... Become a Python developer.
🌐
Pandas
pandas.pydata.org › docs › development › contributing_docstring.html
pandas docstring guide — pandas 3.0.5 documentation
This section follows the same format as the extended summary section. This is one of the most important sections of a docstring, despite being placed in the last position, as often people understand concepts better by example than through accurate explanations. Examples in docstrings, besides illustrating the usage of the function or method, must be valid Python code, that returns the given output in a deterministic way, and that can be copied and run by users.
🌐
pyOpenSci
pyopensci.org › python-package-guide › documentation › write-user-documentation › document-your-code-api-docstrings.html
Document the code in your package’s API using docstrings — Python Packaging Guide
Package APIs consist of functions, classes, methods and attributes that create a user interface. In Python, a docstring refers to text in a function, method or class that describes what the function does and its inputs and outputs.
🌐
Lftechnology
coding-guidelines.lftechnology.com › docstrings
Convention for docstrings | Leapfrog Coding Guidelines
While you can write anything between the triple quotes(""") to write your docstring, it is generally recommended to follow a template for consistency and also for libraries to be able to parse your docstring easily. The official documentation standard for Python is ReStructed Text docstrings (PEP ...
🌐
Lsst
developer.lsst.io › python › numpydoc.html
Documenting Python APIs with docstrings — LSST DM Developer Guide main documentation
For a complete list of sections permitted in constant docstrings see `Documenting Constants and Class Attributes`_. .. _`Documenting Constants and Class Attributes`: https://developer.lsst.io/docs/py_docs.html#py-docstring-attribute-constants-structure """ def moduleLevelFunction(param1, *args, param2=None, **kwargs): """Test that two parameters are not equal. This is an example of a function docstring. Function parameters are documented in the ``Parameters`` section. See *Notes* for the format specification.
🌐
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.