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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
__str__(self)Fill in the Blanks
Spot the Error
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.
Practice this topic interactively, track progress, and unlock badges.
Start Learning Free →