Python help() - Print the documentation (docstring) of a Python function

The help() prints the Python function's documentation (or docstring), which is written between triple double quotes in the declaration. The doc of math.exp(), the function of calculating exponential, can be checked by help().

import math

help(math.exp)

# exp(x, /)
#     Return e raised to the power of x.

The help() prints Return e raised to the power of x..

How to make a doc and call the help() to print it

The docstring occurs normally in the first paragraph and Python (PEP 257) recommends it should be between three double quotes.

def f(x):
    """
    This is doc. This function returns x + 1.
    """
    return x + 1


help(f)
# Help on function f in module __main__:
#
# f(x)
#     This is doc. This function returns x + 1.

doc

The __doc__ returns the docstring of a function.

import math

help(math.exp)

# exp(x, /)
#     Return e raised to the power of x.

print(math.exp.__doc__)
# Return e raised to the power of x.

Python Tips

Python Tutorial