← Python

OOP – String Representation (__str__)

__str__ defines what is displayed when an object is passed to print() or str()

What it does: Python calls __str__ when you use print(obj) or str(obj). __str__ must return a string — f-strings work well here. __str__ defines the human-readable string for the object — used by print(). print(obj) and str(obj) both trigger __str__ automatically. Without __str__, Python uses a default representation showing the class name and memory address. __str__ only produces a string for display — it doesn't alter the object's data. print() calls __str__ — which returns 'Rex', so that's what prints. __str__ has access to self so it can read any of the object's attributes.

Common mistakes: __str__ must RETURN a string, not print it. __str__ must return a string, not an integer.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.

Take this worksheet further — free with Cloodit

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

Learning
Duo
What does __str__ control?
What print(object) displaysHow to create an object
Matches
❌ def __str__(self): return 42
Error: __str__ must return a string
Applying
Meanings
Term
__str__(self)
Meaning
Returns a human-readable string used by print() and str().
Practice
class Student:
Testing
Gaps
def (self):
Check Answer
Spots
2 def __str__(self):
3 print('Dog: ' + self.name)

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

The 'Moose' badge — Tundra Champion, 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. What does __str__ control?
A. What print(object) displays ✓
B. How to create an object
__str__ defines the human-readable string for the object — used by print()
2. When is __str__ called?
A. When you create the object
B. When you print() or str() the object ✓
print(obj) and str(obj) both trigger __str__ automatically
3. What must __str__ return?
A. A string ✓
B. None
__str__ must return a string — Python will raise TypeError if it doesn't
4. What does print(obj) show without __str__?
A. Nothing
B. Something like <ClassName object at 0x...> ✓
Without __str__, Python uses a default representation showing the class name and memory address
5. Does __str__ change the object?
A. No it just returns a string description ✓
B. Yes
__str__ only produces a string for display — it doesn't alter the object's data

Match the Pairs

def __str__(self): return 42 Error: __str__ must return a string
def Str(self): Error: Magic method must be __str__
def __str__(self): Valid: Defines string representation of an object
return f'Dog: {self.name}' Ok: Returns a formatted string from __str__
str(3.14) Output: 3.14
str([1, 2]) Output: [1, 2]

Key Term

__str__(self)
Returns a human-readable string used by print() and str().~~__repr__(self)

Fill in the Blanks

1. class Dog: def __str__(self): return 'Dog: ' + self.name
2. class Dog: def __str__(self): return self.name
3. class Dog: def __str__(self): return 'Dog: ' + self.name
4. dog = Dog('Rex') print(dog)
5. class Point: def __str__(self): return '(' + str(self.x) + ')'

Spot the Error

Question 1
A class Dog:
B def __str__(self):
C print('Dog: ' + self.name)
Line C — __str__ must RETURN a string, not print it
Question 2
A class Dog:
B def __str__(self):
C return 42
Line C — __str__ must return a string, not an integer
Question 3
A class Dog:
B def __str__():
C return 'Dog'
Line B — Missing self parameter
Question 4
A class Dog:
B def _str_(self):
C return 'Dog'
Line B — Need double underscores: __str__ not _str_
Question 5
A class Dog:
B def str(self):
C return 'Dog'
Line B — Must be __str__ with double underscores not just str
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?