← VCE Software Development U3O1

Docstrings for functions and classes

A triple-quoted string on the first line of a function or class body. It documents what the code does, its parameters and its return value. help() displays it.

Understanding Docstrings for functions and classes in VCE Software Development U3O1

What it does: The docstring lists the parameter (price) with its type and meaning, and states what the function returns. The triple-quoted string is the first line indented inside the class, so it documents the Student class. Python only treats a string as the docstring if it is the first statement in the body. help() will not find it elsewhere. help() displays the docstring of a function or class. Functions do not have a .comment attribute. A docstring needs three double quotes (or three single quotes) at each end. Two quotes make an empty string followed by errors.

Common mistakes: The docstring is closed with only two quotes. A triple-quoted docstring must end with three double quotes: """. The function header is missing its colon. It should be def greet(name): so the docstring can sit in the function body.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 48–71) and word search (Modules 10–71).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly starts a docstring for a function?
"""Return the area of a rectangle."""# Return the area of a rectangle.
Matches
def total(a, b): x = a + b """Add two numbers.""" return x
↓
The docstring is not on the first line of the function body, so Python does not treat it as the docstring and help() will not show it. Move it above x = a + b.
Applying
Meanings
Term
"""docstring"""
Meaning
A triple-quoted string on the first line of a function or class body. It documents what the code does, its parameters and its return value. help() displays it.
Practice
def average(a, b):
Testing
Gaps
help()
Check Answer
Spots
2 """Find rectangle area.
3 ""

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Seal' badge — Smooth Formatter, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly starts a docstring for a function?
A. # Return the area of a rectangle.
B. """Return the area of a rectangle.""" ✓
A docstring is a triple-quoted string. A line starting with # is an ordinary comment, not a docstring.
2. Where must a function's docstring be placed?
A. On the first line inside the function body, straight after the def line ✓
B. At the very end of the function, after the return statement
Python only treats a string as the docstring if it is the first statement in the body. help() will not find it elsewhere.
3. Which function shows a docstring to the user in the Python shell?
A. print(calculate_total.comment)
B. help(calculate_total) ✓
help() displays the docstring of a function or class. Functions do not have a .comment attribute.
4. Which docstring best describes a parameter and the return value?
A. """Add tax to price. price: float. Returns the total as a float.""" ✓
B. """This function is very useful."""
A good docstring says what the function does, what each parameter is, and what it returns. Vague praise tells the reader nothing.
5. Which class definition has a docstring in the correct place?
A. class Student: """A student with a name and mark.""" ✓
B. class Student: name = "" """A student with a name and mark."""
For a class, the docstring must be the first statement inside the class body. Here the name assignment comes first, so the string is not a docstring.

Match the Pairs

"""Return the area of a rectangle.""" → A docstring: a triple-quoted string on the first line of a function body that says what the function does
help(calculate_area) → Displays the docstring of the calculate_area function
A docstring describes each parameter and the return value so other programmers know what to pass in and what comes back → """Add GST to a price. price: float, the cost before GST Returns: float, the cost with GST """
A class docstring goes on the first line inside the class body and describes what the class represents → class Student: """A student with a name and a mark."""
def total(a, b): x = a + b """Add two numbers.""" return x → The docstring is not on the first line of the function body, so Python does not treat it as the docstring and help() will not show it. Move it above x = a + b.
def total(a, b): "Add two numbers. Returns the sum." return a + b → Single quotes were used for a multi-line docstring, which causes a syntax error. Use triple quotes at the start and end.

Key Term

"""docstring"""
A triple-quoted string on the first line of a function or class body. It documents what the code does, its parameters and its return value. help() displays it.

Fill in the Blanks

1. class Student: """A student record.""" help(Student)
Hint: Type the name of the class whose docstring you want help() to display
2. def area(w, h): """Return the area.""" help(area)
Hint: Type the built-in function that displays a function's docstring
3. def area(w, h): """Return area. :param w: the width""" return w * h
Hint: Type the docstring tag used to describe a parameter
4. class Student: """A student record.""" count = 0
Hint: Type the keyword that starts a class definition, whose body begins with the docstring
5. def area(w, h): """Find area. :return: the area""" return w * h
Hint: Type the docstring tag used to describe the value a function gives back

Spot the Error

Question 1
A def area(w, h):
B """Find rectangle area.
C ""
D return w * h
Line C — The docstring is closed with only two quotes. A triple-quoted docstring must end with three double quotes: """.
Question 2
A def greet(name)
B """Print a greeting."""
C print("Hi " + name)
Line A — The function header is missing its colon. It should be def greet(name): so the docstring can sit in the function body.
Question 3
A define add(a, b):
B """Return a plus b."""
C return a + b
Line A — Python functions start with the keyword def, not define. The header should be def add(a, b):.
Question 4
A def square(n):
B # Return n squared.
C return n * n
D print(square(4))
Line B — A # comment is not a docstring, so help() cannot show it. The first line of the body should be a triple-quoted string: """Return n squared."""
Question 5
A def hello(name):
B """Print a greeting."""
C print("Hi " + name)
D help(hello())
Line D — help() needs the function object, not the result of calling it. hello() would run without its argument and fail. It should be help(hello).

Student with a class code? Join your class →