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.
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

What are your preferred conventions for documenting python code?
Module docstrings. I don't think I've ever worked with someone else who writes them. Open up a file for the first time, see 30 lines of imports and a bunch of functions that call each other, the file name is generic and provides no context. This tier of missing documentation is really painful. Just explain what should be used outside the module, specific terminology used here and how things link together. I'm tired of building up the design in my mind from all the implementation bits. More on reddit.com
🌐 r/Python
20
22
December 7, 2022
Google Python Style Guide
I don't agree with everything in there but 2 things I like about it. There's a pro/con/final decision for every subject and that is a good way to make up your own mind with all the information. Their docstring format (section 3.8). I just like the simplicity of it. More on reddit.com
🌐 r/Python
10
23
February 11, 2023
What is PEP8 in Python
PEP-8 is basically the official Python style guide. It tells you what your code should ideally look like if following the convention (which is recommended). I assume you meant PEP-257 instead of 256, because the latter is irrelevant. 257 dictates the use of docstrings, which kind of complements PEP-8. While it's a more advanced topic, I'd put PEP-484 in the same category. It explains type hints, which are useful for readability and static type checking with your editor. More on reddit.com
🌐 r/learnpython
8
5
December 1, 2021
Google-style docstring linter
GREAT job. I'm going to give it a try. A few things: I'll probably want to control wether or not an empty docstring raises a warning on a per project basis. Most tools (pytest, mypyt, etc) accept also to put configuration in setup.cfg and tox.ini to centralize conf. you'll need a CLI output mode in a machine friendly format and a pure python documented API so that IDE can integrate that. suggestion of a valid docstring to fix the invalid one would be welcome. eventually people will come to you and ask for numpy and rst style as well. I prefer google style so I don't care. Writting types in annotations and in the docstring is not DRY, so skipping it might be chosen. An option to disable that check on a project basis would be great. More on reddit.com
🌐 r/Python
2
3
October 19, 2017
🌐
Google
google.github.io › styleguide › pyguide.html
Google Python Style Guide
Cite the source of all naming ... or docstring. If the source is not accessible, clearly document the naming conventions. Prefer PEP 8-compliant descriptive_names for public APIs, which are much more likely to be encountered out of context. Use a narrowly-scoped pylint: disable=invalid-name directive to silence warnings. For just a few variables, use the directive as an endline comment for each one; for more, apply the directive at the beginning of a block. In Python, pydoc as ...
🌐
Sphinx
sphinx-doc.org › en › master › usage › extensions › example_google.html
Example Google Style Python Docstrings — Sphinx documentation
Args: param1 (str): Description of `param1`. param2 (:obj:`int`, optional): Description of `param2`. Multiple lines are supported. param3 (list(str)): Description of `param3`. """ self.attr1 = param1 self.attr2 = param2 self.attr3 = param3 #: Doc comment *inline* with attribute #: list(str): Doc comment *before* attribute, with type specified self.attr4 = ['attr4'] self.attr5 = None """str: Docstring *after* attribute, with type specified.""" @property def readonly_property(self): """str: Properties should be documented in their getter method.""" return 'readonly_property' @property def readwrite_property(self): """list(str): Properties with both a getter and setter should only be documented in their getter method.
🌐
DataCamp
datacamp.com › tutorial › docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - One-line docstrings are short descriptions that fit on a single line. They are enclosed in triple quotes (''' or """), and the closing quotes must be on the same line. Although both triple-single and triple-double quotes work, the standard convention in Python is to use triple-double quotes (""").
🌐
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.
Find elsewhere
🌐
Real Python
realpython.com › how-to-write-docstrings-in-python
How to Write Docstrings in Python – Real Python
August 25, 2025 - How should you format docstrings in Python?Show/Hide · You should format docstrings by starting with a concise summary and using triple quotes. For multiline docstrings, include details about parameters and return values.
🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in PEP 257.
🌐
YouTube
youtube.com › watch
Write Python Docstrings Effectively: Understanding & Accessing Docstrings - YouTube
Download your free Python Cheat Sheet here: https://realpython.com/cheatsheetFree Python Skill Test with instant level + learning plan: https://realpython.co...
Published: February 19, 2026
🌐
Stackademic
blog.stackademic.com › mastering-python-docstring-format-a-comprehensive-guide-0b554e8ba28d
Mastering Python Docstring Format: A Comprehensive Guide | by Karim Mirzaguliyev | Stackademic
May 6, 2024 - In this comprehensive guide, we’ll delve deep into Python docstring format, exploring various styles, best practices, and real-world use cases to empower developers in creating exceptional documentation for their projects.
🌐
Stack Abuse
stackabuse.com › common-docstring-formats-in-python
Common Docstring Formats in Python
August 26, 2023 - A docstring, short for documentation ... providing an easy reference for the programmer. In Python, a docstring is a first-class citizen, meaning it can be accessed programmatically using the __doc__ attribute....
🌐
Medium
medium.com › @minto258 › python-rules-of-coding-docstrings-399bcd6448c2
Python Rules Of Coding: Docstrings | by Rahimuddin Alrashel | Medium
June 30, 2020 - Python Rules Of Coding: Docstrings Python docstring (documentation string) is a string literal and it can be used in any code block, i.e., the class, module, function, method definition. It provides …
🌐
Readthedocs
sphinx-rtd-tutorial.readthedocs.io › en › latest › docstrings.html
Writing docstrings — Sphinx-RTD-Tutorial documentation
A pair of :param: and :type: directive options must be used for each parameter we wish to document. The :raises: option is used to describe any errors that are raised by the code, while the :return: and :rtype: options are used to describe any values returned by our code.
🌐
Cornell Computer Science
cs.cornell.edu › courses › cs1110 › 2019fa › resources › style
Python Programming Style
August 24, 2019 - Specifications are docstrings; all other comments are single line comments. You will see a lot of Python code that ignores this guideline.
🌐
Zero To Mastery
zerotomastery.io › blog › python-docstring
Beginner's Guide to Python Docstrings (With Code Examples) | Zero To Mastery
September 27, 2024 - Docstrings are a step up from comments. 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.
🌐
Springer
link.springer.com › home › pro python › chapter
Docstring Conventions | Springer Nature Link
This PEP documents the semantics and conventions associated with Python docstrings.
🌐
YouTube
youtube.com › watch
Docstrings in Python - YouTube
In Python we prefer docstrings to document our code rather than just comments. Docstrings must be the very first statement in their function, class, or modul...
Published: July 24, 2023
🌐
Lingualeo
lingualeo.com › en › jungle › pep-257-docstring-conventions-42218
PEP 257 -- Docstring Conventions translation to English | Lingualeo
Public methods (including the __init__ constructor) should also have docstrings. A package may be documented in the module docstring of the __init__. py file in the package directory.
🌐
Opensourceecon
opensourceecon.github.io › CompMethods › python › DocStrings.html
9. Docstrings and Documentation — Computational Methods for Economists using Python
Docstrings are longer blocks of comments that are set aside to document the source code. Docstrings are usually multi-line and are enclosed in triple quotes """...""". Docstrings are most often used at the top of a module to document what it does and the functions it containts and just after ...