🌐
Python
peps.python.org › pep-0257
PEP 257 – Docstring Conventions | peps.python.org
The docstring in this example contains two newline characters and is therefore 3 lines long.
🌐
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.
🌐
Programiz
programiz.com › python-programming › docstrings
Python Docstrings (With Examples)
For example, "I am a single-line comment" ''' I am a multi-line comment! ''' print("Hello World") Note: We use triple quotation marks for multi-line strings. ... As mentioned above, Python docstrings are strings used right after the definition of a function, method, class, or module (like in ...
🌐
DataCamp
datacamp.com › tutorial › docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - In Python, you can access a docstring using the __doc__ attribute of the object. For example, you could access the docstring for a function using my_function.__doc__or the docstring for a class using MyClass.__doc__.
🌐
Mimo
mimo.org › glossary › python › docstrings
Python Docstrings: Syntax, Usage, and Examples
Docstrings in classes describe their attributes and methods. They’re especially handy for explaining a constructor (init) when your class is complex. ... class Car: """ Represents a car. Attributes: make (str): The car's brand. model (str): The car's model. year (int): The year of manufacture. """ def __init__(self, make, model, year): self.make = make self.model = model self.year = year · Python modules should include a docstring at the beginning to explain their purpose.
🌐
FavTutor
favtutor.com › blogs › docstring-python
Python Docstring: How to Write Docstrings? (with Examples)
June 6, 2023 - An example of a multi-line docstring is shown here: class Rectangle: """ This class represents a rectangle. Attributes: width (int): The width of the rectangle. height (int): The height of the rectangle.
🌐
AskPython
askpython.com › python › python-docstring
Python Docstring - AskPython
February 16, 2023 - $ python ls docstrings.py $ python $ python python3.7 Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> >>> import docstrings >>> >>> docstrings.__doc__ '\nThis module shows some examples of Python Docstrings\n\nClasses: Employee\nFunctions: multiply(a, b)\n' >>> >>> docstrings.Employee.__doc__ 'Employee class is used to hold employee object data.\n\n Methods:\n __init__(self, emp_id, emp_name)\n print()\n ' >>> >>> >>> docstrings.multiply.__doc__ 'This method multiplies the given two numbers.\n\n Input Arguments: a, b must be numbers.\n Returns: Multiplication of a and b.\n ' >>> >>> >>> docstrings.Employee.print.__doc__ 'This method prints the employee information in a user friendly way.'
🌐
Sphinx
sphinx-doc.org › en › master › usage › extensions › example_google.html
Example Google Style Python Docstrings — Sphinx documentation
If attribute, parameter, and return types are annotated according to `PEP 484`_, they do not need to be included in the docstring: Args: param1 (int): The first parameter. param2 (str): The second parameter. Returns: bool: The return value. True for success, False otherwise. """ def function_with_pep484_type_annotations(param1: int, param2: str) -> bool: """Example function with PEP 484 type annotations.
🌐
Real Python
realpython.com › how-to-write-docstrings-in-python
How to Write Docstrings in Python – Real Python
August 25, 2025 - When you write docstrings for modules, the goal is to provide a high-level summary of what the program does. This appears at the top of your Python file and serves as an overview of its contents. Here, you’ll add a brief description of the module’s purpose and a list of its components. You could also add references to related modules or examples of usage.
Find elsewhere
🌐
Python-sprints
python-sprints.github.io › pandas › guide › pandas_docstring.html
pandas docstring guide — Python documentation
Parameters ---------- num1 : int First number to add num2 : int Second number to add Returns ------- int The sum of `num1` and `num2` See Also -------- subtract : Subtract one integer from another Examples -------- >>> add(2, 2) 4 >>> add(25, 0) 25 >>> add(10, -10) 0 """ return num1 + num2 ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python Docstring Formats (Styles) and Examples | note.nkmk.me
August 26, 2023 - Specify types with docstrings | PyCharm Documentation ... def func_rest(param1, param2): """Summary line. :param param1: Description of param1 :type param1: int :param param2: Description of param2 :type param2: str :returns: Description of ...
🌐
Software Testing Help
softwaretestinghelp.com › home › python › python docstring: documenting and introspecting functions
Python Docstring: Documenting And Introspecting Functions
April 1, 2025 - At a minimum, a Python docstring should give a quick summary of whatever the function is doing. A function’s docstring can be accessed in two ways. Either directly via the function’s __doc__ special attribute or using the built-in help() function which accesses __doc__ behind the hood. Example 1: Access a function’s docstring via the function’s __doc__ special attribute.
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
🌐
PythonForBeginners.com
pythonforbeginners.com › home › python docstrings
Python Docstrings - PythonForBeginners.com
August 28, 2020 - Let’s show how an example of a multi-line docstring: def my_function(): """Do nothing, but document it. No, really, it doesn't do anything. """ pass · Let’s see how this would look like when we print it · >>> print my_function.__doc__ Do nothing, but document it. No, really, it doesn't do anything. The following Python file shows the declaration of docstrings within a python source file:
🌐
Zero To Mastery
zerotomastery.io › blog › python-docstring
Beginner's Guide to Python Docstrings (With Code Examples) | Zero To Mastery
September 27, 2024 - Struggling with code documentation? Learn how to write Python docstrings for better readability and maintainability, with automated documentation.
🌐
Readthedocs
sphinxcontrib-napoleon.readthedocs.io › en › latest › example_google.html
Example Google Style Python Docstrings — napoleon 0.7 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.
🌐
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 support the action we want it to perform. It was a simple example...
🌐
Python Land
python.land › home › language deep dives › python docstring: documenting your code
Python Docstring: Documenting Your Code • Python Land Tutorial
May 10, 2022 - As defined above, we can simply insert a string as the first statement of any module, function, class, or method and it becomes the docstring. Here’s an example of how to document a Python class and its functions using docstrings:
🌐
Wikipedia
en.wikipedia.org › wiki › Docstring
Docstring - Wikipedia
December 19, 2025 - When they are kept, docstrings may be viewed and changed using the DOCUMENTATION function. For instance: (defun foo () "hi there" nil) (documentation #'foo 'function) => "hi there" In Python, a docstring is a string literal that follows a module, class or function definition.
🌐
Pandas
pandas.pydata.org › docs › development › contributing_docstring.html
pandas docstring guide — pandas 3.0.6 documentation
Examples -------- >>> add(2, 2) 4 >>> add(25, 0) 25 >>> add(10, -10) 0 """ return num1 + num2 · Some standards regarding docstrings exist, which make them easier to read, and allow them be easily exported to other formats such as html or pdf. The first conventions every Python docstring should follow are defined in PEP-257.