What is a docstring in Python?

A docstring is a string written in triple quotes as the first statement of a function, class or module. It explains what that code does.

def add(a, b):
    """Return the sum of a and b."""
    return a + b

print(add.__doc__)

Output:

Tutorials Class - Output Window

Return the sum of a and b.

A docstring is different from a comment. A comment (starting with #) is ignored by Python, but a docstring is stored with the object. You can read it with __doc__ or with the help() function. See Python Comments for more.


Learn more about the similar topics:
Tutorials
Python Functions
Python Built-in Functions
Scope of Variables in Python
Pass function as an argument in Python
Exercises & Assignments
Python calculator program using functions
Interview Questions & Answers
What is a docstring in Python?
What is scope in Python?
What are *args and **kwargs in Python?