๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ trypython.asp
W3Schools online PYTHON editor
โฏRun Code Ctrl+Alt+R Change Orientation Ctrl+Alt+O Change Theme Ctrl+Alt+D Go to Spaces Ctrl+Alt+P
๐ŸŒ
Python
peps.python.org โ€บ pep-0257
PEP 257 โ€“ Docstring Conventions | peps.python.org
Any indentation in the first line of the docstring (i.e., up to the first newline) is insignificant and removed. Relative indentation of later lines in the docstring is retained. Blank lines should be removed from the beginning and end of the docstring. Since code is much more precise than words, here is an implementation of the algorithm:
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ docstrings
Python Docstrings (With Examples)
In this tutorial, we will learn about Python docstrings. More specifically, we will learn how and why docstrings are used with the help of examples.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ development โ€บ contributing_docstring.html
pandas docstring guide โ€” pandas 3.0.2 documentation
def add_values(arr): """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum(arr) ... def func(): """Some function. With several mistakes in the docstring. It has a blank line after the signature ``def func():``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and also with the built-in help() function. An object's docstring is defined by including a string constant as the first statement in the object's definition. Unlike regular comments, which explain individual ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Real Python
realpython.com โ€บ documenting-python-code
Documenting Python Code: A Complete Guide โ€“ Real Python
December 21, 2023 - Now that weโ€™ve learned about commenting, letโ€™s take a deep dive into documenting a Python code base. In this section, youโ€™ll learn about docstrings and how to use them for documentation.
Find elsewhere
Top answer
1 of 3
27

The statements written with >>> in the docstrings are doctests.

It lets you test your code by running examples embedded in the documentation and verifying that they produce the expected results. It parses the help text to find examples, runs them and then compares the output text against the expected value.

In your case, PyCharm has done the extra task of highlighting the python code in the docstrings. It won't affect your normal function execution so you don't need to worry about it.

Example:
Lets say I have a script named doctest_simple_addition in which i have written some doctests for add() function where some test cases gives proper output and some raises an exception. Then i can verify that my function produces the expected results by running those doctests.

doctest_simple_addition.py

def add(a,b):
    """
    >>> add(1, 2)
    3

    >>> add(5, 3)
    8

    >>> add('a', 1)
    Traceback (most recent call last):
        ...
    TypeError: cannot concatenate 'str' and 'int' objects
    """

    return a + b

To run the doctests, use doctest as the main program via the -m option to the interpreter. Usually, no output is produced while the tests are running. You can add the -v option and doctest will then print a detailed log of what itโ€™s trying with a summary at the end.

Doctest looks for lines beginning with the interpreter prompt, >>>, to find the beginning of a test case. The test case is ended by a blank line, or by the next interpreter prompt.

$ python -m doctest -v doctest_simple_addition.py 

Trying:
    add(1, 2)
Expecting:
    3
ok
Trying:
    add(5, 3)
Expecting:
    8
ok
Trying:
    add('a', 1)
Expecting:
    Traceback (most recent call last):
        ...
    TypeError: cannot concatenate 'str' and 'int' objects
ok
1 items had no tests:
    doctest_simple_addition
1 items passed all tests:
   3 tests in doctest_simple_addition.add
3 tests in 2 items.
3 passed and 0 failed.
Test passed.

Note: When doctest sees a traceback header line (either Traceback (most recent call last): or Traceback (innermost last):, depending on the version of Python you are running), it skips ahead to find the exception type and message, ignoring the intervening lines entirely.
This is done because paths in a traceback depend on the location where a module is installed on the filesystem on a given system and it would be impossible to write portable tests as the path would change from system to system.

2 of 3
5

Your intuition is correct, they are to be executed. But don't worry, they are doctest strings. They won't interfere with the normal execution of a module, so everything is fine. PyCharm is just being helpful by recognizing them.

๐ŸŒ
Dataquest
dataquest.io โ€บ blog โ€บ documenting-in-python-with-docstrings
Tutorial: Documenting in Python with Docstrings
December 13, 2024 - Documenting your code is a critical skill for any data scientist or software engineer. This tutorial will show you how using docstrings.
๐ŸŒ
Readthedocs
sphinxcontrib-napoleon.readthedocs.io โ€บ en โ€บ latest โ€บ example_google.html
Example Google Style Python Docstrings โ€” napoleon 0.7 documentation
Attributes: msg (str): Human readable string describing the exception. code (int): Exception error code. """ 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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-docstrings
Python Docstrings - GeeksforGeeks
August 2, 2024 - Python documentation strings (or ... modules, functions, classes, and methods. It's specified in source code that is used, like a comment, to document a specific segment of code....
๐ŸŒ
PythonForBeginners.com
pythonforbeginners.com โ€บ home โ€บ python docstrings
Python Docstrings - PythonForBeginners.com
August 28, 2020 - Unlike conventional source code comments the docstring should describe what the function does, not how. All functions should have a docstring This allows the program to inspect these comments at run time, for instance as an interactive help system, or as metadata.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ docstrings
Mimo: The coding platform you need to learn Web Development, Python, and more.
Can appear anywhere in the code. Use the # symbol before the text. ... Document modules, classes, and functions. Are accessible at runtime through help(). Go right after the definition line of a function, class, or module. Use triple quotes to enclose the text.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Docstring
Docstring - Wikipedia
December 19, 2025 - In Python, a docstring is a string literal that follows a module, class or function definition. It must be nothing but a string literal, not any other kind of expression. The docstring is accessible via the associated code element's __doc__ attribute and the help function.
๐ŸŒ
Zencoder
zencoder.ai โ€บ home โ€บ python docstring guide: format, examples & best practices
Python Docstring Guide: Format, Examples & Best Practices
December 3, 2025 - A Python docstring is a string literal that appears as the first statement in a module, function, class, or method. It serves as documentation for that component and can be accessed programmatically via the __doc__ attribute. def add(a, b): ...
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ glossary โ€บ docstring
docstring | Python Glossary โ€“ Real Python
They serve as an immediate reference for anyone who needs to understand or use the code, including future you! Hereโ€™s an example of how you can use docstrings to document a function: ... def add(a, b): """Sum two numbers. Args: a (int or float): The first number. b (int or float): The second number. Returns: int or float: The sum of the two numbers. """ return a + b ยท In this example, the docstring explains what the add() function does and describes its arguments and return value. ... A complete guide to documenting Python code.
๐ŸŒ
Python Land
python.land โ€บ home โ€บ language deep dives โ€บ python docstring: documenting your code
Python Docstring: Documenting Your Code โ€ข Python Land Tutorial
May 10, 2022 - It can be used as a way to formally document your code, as youโ€™ll soon learn. 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.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ docstring-python
Python Docstring: How to Write Docstrings? (with Examples)
June 6, 2023 - Triple quotes are used for writing docstrings in Python. A concise explanation of what the code does should be on the first line, followed by a blank line.
๐ŸŒ
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.