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
If a class subclasses another class and its behavior is mostly inherited from that class, its docstring should mention this and summarize the differences. Use the verb โ€œoverrideโ€ to indicate that a subclass method replaces a superclass method and does not call the superclass method; use the verb โ€œextendโ€ to indicate that a subclass method calls the superclass method (in addition to its own behavior). Do not use the Emacs convention of mentioning the arguments of functions or methods in upper case in running text. Python is case sensitive and the argument names can be used for keyword arguments, so the docstring should document the correct argument names.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Most common style in Python. Uses triple single (''') or triple double (""") quotes. Can span multiple lines. Example 1: This function shows how to use triple single quotes for docstrings.
Discussions

Advice on writing some docstrings
What it means is that self._instance ... be something that has methods copy() and append(), but this kind of mistake is very typical of Python as of late. ... When writing docstrings you should adhere to some style.... More on reddit.com
๐ŸŒ r/learnpython
7
3
May 6, 2022
coding style - What are the most common Python docstring formats? - Stack Overflow
I have seen a few different styles of writing docstrings in Python, what are the most popular styles? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Docstrings in Python - software engineering - Discuss Career & Computing - OpenGenus
In this article we'll go through the Docstrings in Python. Docstrings stand for Documentation Strings. Docstrings like comments provide us a convenient way of associating documentation to the source code. Unlike comments docstrings are not stripped but are retained through out the runtime of ... More on discourse.opengenus.org
๐ŸŒ discourse.opengenus.org
0
July 8, 2019
Docstring vs Comments
Docstrings are easily obtainable by other Python tools dynamically just by inspecting your objects. This is useful for tools that do things like generating API documentation. Comments are, by comparison, more difficult for such tools to use in part because comments are discarded by the compiler whereas docstrings are a part of your object (see .__doc__ attribute of any function, class, etc.). They're not necessarily interchangable tools, however. They're different tools for different purposes. Also, docstrings only work in certain places like at the very beginning of modules, classes, or functions. Comments, on the other hand, can be placed anywhere. More on reddit.com
๐ŸŒ r/learnpython
4
8
September 11, 2024
๐ŸŒ
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.
๐ŸŒ
Sphinx
sphinx-doc.org โ€บ en โ€บ master โ€บ usage โ€บ extensions โ€บ example_google.html
Example Google Style Python Docstrings โ€” Sphinx documentation
Properties created with the ``@property`` decorator should be documented in the property's getter method. Attributes: attr1 (str): Description of `attr1`. attr2 (:obj:`int`, optional): Description of `attr2`. """ def __init__(self, param1, param2, param3): """Example of docstring on the __init__ method.
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ python โ€บ python docstring: documenting and introspecting functions
Python Docstring: Documenting And Introspecting Functions
April 1, 2025 - This tutorial explains what is Python Docstring and how to use it to document Python functions with examples. Includes function introspecting.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ advice on writing some docstrings
r/learnpython on Reddit: Advice on writing some docstrings
May 6, 2022 - What it means is that self._instance ... be something that has methods copy() and append(), but this kind of mistake is very typical of Python as of late. ... When writing docstrings you should adhere to some style....
Find elsewhere
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
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ development โ€บ contributing_docstring.html
pandas docstring guide โ€” pandas 3.0.1 documentation
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.
๐ŸŒ
Medium
medium.com โ€บ geekculture โ€บ the-importance-of-writing-clear-docstrings-in-programming-62bffffa3881
Python best practices: Docstrings | by Ethan Jones | Geek Culture | Medium
February 8, 2024 - Such a docstring becomes the __doc__ special attribute of that object. It is used to describe the functionality of a module or a function including the parameters and return, should they be present. In my many encounters with teaching material for Python, I have never come across any content explaining what docstrings are and the advantages of using them.
๐ŸŒ
Python
peps.python.org โ€บ pep-0008
PEP 8 โ€“ Style Guide for Python Code | peps.python.org
For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.
๐ŸŒ
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.
๐ŸŒ
Opengenus
discourse.opengenus.org โ€บ software engineering
Docstrings in Python - software engineering - Discuss Career & Computing - OpenGenus
July 8, 2019 - In this article we'll go through the Docstrings in Python. Docstrings stand for Documentation Strings. Docstrings like comments provide us a convenient way of associating documentation to the source code.
๐ŸŒ
GitHub
gist.github.com โ€บ nipunsadvilkar โ€บ fec9d2a40f9c83ea7fd97be59261c400
What is the standard Python docstring format? ยท GitHub
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).
๐ŸŒ
Lsst
developer.lsst.io โ€บ python โ€บ numpydoc.html
Documenting Python APIs with docstrings โ€” LSST DM Developer Guide main documentation
For docstrings, the Numpydoc standard is to omit any space between a header and the following paragraph. ... """A summary sentence. Notes ----- The content of the notes section directly follows the header, with no blank line. """ This deviation from the normal style guide is in keeping with Python community idioms and to save vertical space in terminal help printouts.
๐ŸŒ
Plain English
python.plainenglish.io โ€บ python-docstrings-your-key-to-clear-maintainable-and-collaborative-code-67d8dffad53d
Python Docstrings: Your Key to Clear, Maintainable, and Collaborative Code
January 29, 2024 - Follow to join our 3.5M+ monthly readers. ... A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. It is used to document the purpose and usage of the code.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_decorators.asp
Python Decorators
Import functools.wraps to preserve the original function name and docstring.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what is the most commonly used docstring format?
r/learnpython on Reddit: What is the most commonly used docstring format?
April 20, 2021 -

I've been using Sphinx (not the program, just the docstring format) recently but my editor doesn't format it correctly, and by default only supports Epydoc and ReStructuredText. To add to this confusion, on program Sphinx's website they list ReStructuredText as their default markdown syntax.

Is there a standard or conventional approach to writing docstrings I am missing?

๐ŸŒ
Zero To Mastery
zerotomastery.io โ€บ blog โ€บ python-docstring
Beginner's Guide to Python Docstrings (With Code Examples) | Zero To Mastery
September 27, 2024 - Think of them as mini-explanations that stick with your functions, classes, or modules. They live inside your code, but theyโ€™re also accessible through Python's help() function, making them perfect for creating formal documentation.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ docstrings - should main() method have an examples section?
r/learnpython on Reddit: Docstrings - should main() method have an examples section?
December 7, 2020 -

Hello, all!

I've been working on my docstrings skills and I've ran into the problem of how to write a docstring for my main method. I couldn't find much with my google fu, so I'm thinking that this might fall to an opinion question.

So, if my driver script (named prog.py) looks like

#! python

""" prog.py """

def main():
    """
    Main method.

    Main method ran when executing the prog.py file from console.
    This method is not designed to be called like a method from a module.

    Parameters
    -------------
    None

    Returns
    ---------
    None

    Raises
    -------
    None

    Examples
    -----------
    ????????????
    """

    pass

if __name__ == '__main__':

    main()

If I never intend to allow my main method to be imported and ran like a module method, do you have any suggestions on how to write the examples section of the docstring?

Do you feel that a docstring is not needed for the main method()?

Thank you so much in advance for your opinions and advice!

EDIT: Formatting and engrish.