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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.