Definition
A string literal which appears as the first expression in a class,
function or module. While ignored when the suite is executed, it is
recognized by the compiler and put into the __doc__ attribute
of the enclosing class, function or module. Since it is available via
introspection, it is the canonical place for documentation of the
object.
Official Documentation Link
Example
def add_numbers(a, b):
"""
This function adds two numbers and returns the result.
Parameters:
a (int, float): The first number to add
b (int, float): The second number to add
Returns:
int, float: The sum of a and b
"""
return a + b
In this example, add_numbers is a simple function that takes two numbers (a and b) as parameters and returns their sum. The docstring is the text enclosed in triple quotes just below the function definition.
The docstring explains what the function does, what parameters it takes (along with their expected types), and what it returns. This information can be extremely useful for anyone reading the code or using the function, as it explains the function’s purpose and expected usage.