← VCE Applied Computing U1O2

Docstrings for functions and classes

A triple-quoted string placed first in a function or class that describes its purpose, parameters and return value. It is internal documentation.

Understanding Docstrings for functions and classes in VCE Applied Computing U1O2

What it does: The docstring describes the purpose, lists the parameters a and b, and states what is returned. The triple-quoted string is the first statement in the class body, which makes it a docstring rather than an ordinary comment. A docstring is internal documentation. It tells other programmers what the function does, what it takes in and what it gives back. Docstrings live inside the source code, so they are internal documentation that helps with maintenance. A function docstring sits directly under the def line and explains the purpose and return value of the function. The convention is to use triple quotes for docstrings, so the description needs to be wrapped in """ at both ends.

Common mistakes: A multi-line docstring must end with three double quotes. Ending with only two leaves the string unterminated, so it should be Returns the sum.""". A docstring uses triple quotes. A single-quoted string is not a docstring, so it should be """Greet a person by name.""".

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 89–124) and word search (Modules 8–124).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which line is a correct way to start a function docstring in Python?
"""Return the area of a rectangle.""""Return the area of a rectangle."
Matches
def area(w, h): x = w * h """Return the area.""" return x
↓
The docstring is not the first statement in the function. It must come straight after the def line, before any other code.
Applying
Meanings
Term
"""docstring"""
Meaning
A triple-quoted string placed first in a function or class that describes its purpose, parameters and return value. It is internal documentation.
Practice
def average(a, b):
Testing
Gaps
area(w, h):
Check Answer
Spots
2 """Add two numbers.
3 Returns the sum.""

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 is a correct way to start a function docstring in Python?
A. "Return the area of a rectangle."
B. """Return the area of a rectangle.""" ✓
A docstring uses triple quotes (""" or '''). Single quotes make an ordinary string, not a docstring.
2. Where must a function docstring be placed?
A. On the first line inside the function, directly under def ✓
B. At the very end of the function, after the return
Python only treats a string as the docstring if it is the first statement in the function body.
3. What does a function docstring typically describe?
A. Only the colour of the program's interface
B. Its purpose, parameters and return value ✓
A docstring is internal documentation. It tells other programmers what the function does, what it takes in and what it gives back.
4. Which is a class docstring?
A. class Student: """A student with a name and a mark.""" ✓
B. """A student with a name and a mark.""" class Student:
A class docstring goes inside the class, as the first statement under the class line, not above it.
5. Which statement about docstrings is true?
A. A docstring is a form of internal documentation inside the code ✓
B. A docstring is a separate user manual stored outside the code
Docstrings live inside the source code, so they are internal documentation that helps with maintenance.

Match the Pairs

def area(w, h): """Return the area of a rectangle.""" → A function docstring: a triple-quoted string on the first line inside the function that describes what it does and returns
class Student: """A student with a name and a mark.""" → A class docstring: a triple-quoted string on the first line inside the class that describes what the class represents
A function docstring can list the parameters and the return value so other programmers know how to use it → def add(a, b): """Add two numbers. a, b: numbers to add Returns: their sum"""
A docstring is written with triple quotes as the first statement inside the function or class body → class Dog: """A pet dog with a name and age."""
def area(w, h): x = w * h """Return the area.""" return x → The docstring is not the first statement in the function. It must come straight after the def line, before any other code.
class Car: "A car with a make and a speed." def __init__(self, make): → The docstring uses single double quotes instead of triple quotes. Docstrings should use triple quotes, as in """text""".

Key Term

"""docstring"""
A triple-quoted string placed first in a function or class that describes its purpose, parameters and return value. It is internal documentation.

Fill in the Blanks

1. def area(w, h): """Return the area of a rectangle.""" return w * h
Hint: Type the keyword that starts a function definition.
2. class Player: """A player in the game.""" lives = 3
Hint: Type the keyword that starts a class definition.
3. def area(w, h): """Return the area of a rectangle.""" return w * h
Hint: Type the keyword that sends the result back, as the docstring says.
4. class Dog: """A pet dog with a name.""" def bark(self):
Hint: Type the first parameter that every method in a class receives.
5. def area(w, h): """Return the area of a rectangle.""" print(area.__doc__)
Hint: Type the special attribute that stores a function's docstring text.

Spot the Error

Question 1
A def add(a, b):
B """Add two numbers.
C Returns the sum.""
D return a + b
Line C — A multi-line docstring must end with three double quotes. Ending with only two leaves the string unterminated, so it should be Returns the sum.""".
Question 2
A def greet(name):
B "Greet a person by name."
C print("Hello " + name)
Line B — A docstring uses triple quotes. A single-quoted string is not a docstring, so it should be """Greet a person by name.""".
Question 3
A def square(n)
B """Return n squared."""
C return n * n
Line A — The function header is missing its colon. It should be def square(n): so the docstring can sit inside the function.
Question 4
A def area(w, h):
B """Return the area."""
C return w + h
Line C — The docstring says the function returns the area, but the code adds the sides. It should be return w * h so the code matches its documentation.
Question 5
A class Pet:
B # A pet with a name.
C def __init__(self, name):
D self.name = name
Line B — A class docstring must be a triple-quoted string, not a # comment. It should be """A pet with a name.""".

Student with a class code? Join your class →