In Python, help(obj) prints documentation for any object: its signature and its docstring, the string literal at the top of a function, class, or module. help(len) explains len(), and help("keywords") lists Python keywords.
Called with no argument, it starts an interactive help prompt in the shell. It is powered by the standard pydoc module, which can also render the same text into a string with pydoc.render_doc().
help() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Pass an object, or a string to search for a topic.
Help on built-in function len in module builtins:
len(obj, /)
Return the number of items in a container.
Help text varies by Python version; this is typical recent CPython output.
How It Works
Header line. It names the object and the module it comes from, builtins.
Signature.len(obj, /) shows one positional-only parameter.
Docstring. The indented text is the function's documentation.
Version differences. Wording and layout can change between Python releases.
Example 2 — help() Shows Your Own Docstrings
Write a function with a docstring and ask for help on it.
python
def area(width, height):
"""Return the area of a rectangle.
Both arguments must be numbers.
"""
return width * height
help(area)
Output
Help on function area in module __main__:
area(width, height)
Return the area of a rectangle.
Both arguments must be numbers.
Help text varies by Python version (output shortened: a trailing blank line is omitted).
How It Works
Docstring first. The triple-quoted string right after def becomes area.__doc__.
Script module. Run directly, the function lives in the module __main__.
Cleaned indentation. help() removes the docstring's leading indentation before printing.
Summary line. Keep the first line short; tools show it in lists.
Example 3 — Get Documentation as a String with pydoc
Render class documentation into a string, check it, and read a docstring directly.
python
import pydoc
class Greeter:
"""Say hello in different styles."""
def hello(self, name):
"""Return a friendly greeting."""
return "Hello, " + name
text = pydoc.render_doc(Greeter, renderer=pydoc.plaintext)
lines = text.splitlines()
print(lines[0])
print("Return a friendly greeting." in text)
print(Greeter.hello.__doc__)
Output
Python Library Documentation: class Greeter in module __main__
True
Return a friendly greeting.
The full rendered page is long and its layout varies by Python version, so only selected parts are printed.
How It Works
No printing, no pager.pydoc.render_doc() returns the help text as a string.
Plain text.renderer=pydoc.plaintext avoids the bold formatting used in terminals.
Search the text. The method's docstring is part of the rendered page.
Direct access.__doc__ gives the raw docstring when that is all you need.
Frequently Asked Questions
Call help(obj) with any function, class, module, or method, or help("topic") for keywords and topics.
Press q to leave the pager. In the interactive help> prompt, type quit or press Enter on an empty line.
From docstrings and signatures, formatted by the pydoc module.
Put a docstring as the first statement of your function, class, or module.
The wording varies by Python version and terminal. The content comes from the same docstrings.