🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Docstrings (Documentation Strings) are special strings used to document Python code. They provide a description of what a module, class, function or method does.
Discussions

python - Getting the docstring from a function - Stack Overflow
I have the following function: def my_func(): """My docstring is both funny and informative""" pass How do I get access to the docstring? More on stackoverflow.com
🌐 stackoverflow.com
coding style - What are the most common Python docstring formats? - Stack Overflow
I find the "signature in docstrings"-style awfully redundant and verbose. For Python 3+, Function annotations are a much cleaner way to do this. Even worse if it uses pseudo-strong-types: Python is way better with duck typing. More on stackoverflow.com
🌐 stackoverflow.com
Propper way to write DocStrings
There's this https://peps.python.org/pep-0257/ More on reddit.com
🌐 r/learnpython
5
1
January 5, 2023
What is the most commonly used docstring format?
I personally follow the Google docstrings More on reddit.com
🌐 r/learnpython
4
4
April 20, 2021
🌐
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.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python function docstrings
Python Function Docstrings
March 26, 2025 - You'll learn how to use Python function docstrings to add documentation to functions and use the help function to retrieve it.
🌐
DataCamp
datacamp.com › tutorial › docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Python documentation string, commonly known as docstring, is a string literal, and it is used in the class, module, function, or method definition. Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and also with the built-in help() function.
🌐
Python-sprints
python-sprints.github.io › pandas › guide › pandas_docstring.html
pandas docstring guide — Python 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.
🌐
Mimo
mimo.org › glossary › python › docstrings
Python Docstrings: Syntax, Usage, and Examples
Use Python docstrings to document functions, classes, and modules. Access them with help(), generate documentation, and improve code readability.
🌐
Dataquest
dataquest.io › home › blog › how to use python docstrings for effective code documentation
Tutorial: Documenting in Python with Docstrings
December 13, 2024 - The Python docstring of this function is enclosed between three double quotes from both sides. As you can see, this string explains what this function does and indicates how we can change its functionality — and what happens if it doesn't ...
Find elsewhere
🌐
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.
🌐
AskPython
askpython.com › python › python-docstring
Python Docstring - AskPython
February 16, 2023 - Python Docstring (Document String) is a string literal that is the first statement in a module, function, class, or 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.
🌐
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, ...
🌐
PythonForBeginners.com
pythonforbeginners.com › home › python docstrings
Python Docstrings - PythonForBeginners.com
August 28, 2020 - Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods.
🌐
Medium
medium.com › @syedar.sohail › docstring-and-why-is-it-important-python-classes-modules-and-functions-95fee5247ff5
Docstring and why is it important ? — Python Classes, Modules and Functions | by Sohail | Medium
October 30, 2022 - Please do not confuse thinking comments and Docstrings are the same. well, they may highly look similar in the way they work but there is a lot of difference. Comments are written generally to show some unusual portions of code and for fixing the bugs. While Docstrings are the right tool for documenting the classes, functions, modules and packages.
Top answer
1 of 4
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 4
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
🌐
Real Python
realpython.com › lessons › function-docstrings
Function Docstrings (Video) – Real Python
A docstring is created when the first line under a function’s header is a string literal.
Published: July 6, 2021
🌐
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 ...
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › python › python_docstrings.htm
Python - Docstrings
Docstrings in Python are accessed using the __doc__ attribute of the object they document. This attribute contains the documentation string (docstring) associated with the object, providing a way to access and display information about the purpose ...