Stop 04 · FunctionsTopics · 8/8

Docstrings

Part of the Python Roadmap

In short

A good function name says what it does. A docstring says how to use it: what goes in, what comes out and what to watch for. Editors, help() and documentation tools all read it for free.

A docstring is a string placed as the first line of a function, class or module that explains what it does, its parameters and what it returns. Tools like help() and VS Code show it automatically.

The jaadu unfolds a long paper leaflet pulled from a small medicine box, reading it carefully through its big round glasses.

Think of it like this

The leaflet inside a medicine box

Every medicine box has a folded leaflet: what it's for, the dose, and warnings. You don't need to know the chemistry to use it safely. A docstring is that leaflet for your function. Anyone can call help() and learn how to use it without reading the code inside.

Watch it happen

Comment or docstring?

# Comment

  • Starts with #, anywhere in the code
  • Explains why a tricky line exists
  • For people reading the source
  • Ignored completely by Python

"""Docstring"""

  • A string as the first statement of a function, class or module
  • Explains what it does and how to call it
  • For people using the function
  • Stored in __doc__ and shown by help() and editors

Use docstrings for the public "how to use me" and comments for the internal "why I did it this way".

The big ideas

What you need to know

Triple quotes, first line

Put a """string""" right under the def line. Placed anywhere else, it's just an unused string, not a docstring.

One-line summary first

Start with a short sentence in the imperative, like "Return each person's share of a bill." Add details after a blank line.

Describe inputs and outputs

List parameters, what it returns and any errors it raises. The Google style with Args: and Returns: sections is popular and easy to read.

help() reads it

help(split_bill) prints the docstring, and VS Code shows it when you hover over a call. Good docstrings save teammates time.

See it in code

Walk through the code

python
1def split_bill(total, people, tip_percent=0):2    """Return each person's share of a bill.3 4    Args:5        total: Bill amount in rupees, before tip.6        people: Number of people sharing. Must be at least 1.7        tip_percent: Optional tip as a percentage of total.8 9    Returns:10        The amount each person pays, rounded to 2 decimals.11    """12    with_tip = total * (1 + tip_percent / 100)13    return round(with_tip / people, 2)14 15 16print(split_bill(1800, 4, tip_percent=10))   # 495.017print(split_bill.__doc__.splitlines()[0])     # Return each person's share of a bill.18help(split_bill)                              # shows the full docstring

1/5The summary line: one sentence, in the imperative, ending with a full stop.

A Google-style docstring. Hover over split_bill in VS Code to see it appear as a tooltip.

Save yourself the pain

Common mistakes

Oops

Writing the docstring as a # comment above the def line.

Do this instead

Put a triple-quoted string as the first line inside the function, so help() and editors can find it.

Oops

A docstring that repeats the name: """Split bill function."""

Do this instead

Say what it returns and how to use it: units, valid ranges and edge cases.

Oops

Updating the code but not the docstring.

Do this instead

Treat the docstring as part of the function. A wrong docstring is worse than none.

Your mission

Document your functions

~20 min

Quick check

Where must a docstring be placed for help() to show it?

Go deeper

  1. 1Add a one-line docstring to every function you write this week.
  2. 2Run help(your_function) in the REPL to see your docstring.
  3. 3Follow one format, such as Google style, for describing arguments and return values.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge