✦ Python Intermediate & OOP Collections module

Concept 04 · Special Methods

Let Python understand your objects.

How does Python know what to display when we print an object or ask for its length?

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Special methods let objects work naturally with print() and len().

How does Python know what to display when we print an object or ask for its length?

class Book:
    def __init__(self, title, author): self.title, self.author = title, author
    def __str__(self): return f"{self.title} by {self.author}"
book = Book("Dune", "Frank Herbert")
print(book)

class BookCollection:
    def __init__(self): self.books = []
    def add_book(self, book): self.books.append(book)
    def __len__(self): return len(self.books)
collection = BookCollection()
for title in ("Dune", "The Hobbit", "Python Basics"): collection.add_book(title)
print(len(collection))

Without __str__(), print(book) usually shows an unreadable default object description. __str__() returns readable text for print(book) and str(book); it must use return, not print(). BookCollection stores books in a list, and len(collection) uses its __len__() method to return a non-negative integer. This prepares us for a Library of real Book objects in the next Composition lesson. __str__() returns a string, while __len__() returns a non-negative integer.

Dune by Frank Herbert 3

Try changing one value, then explain which part of the collection changed and why.

↳

Keep this in mind: Read the collection operation before predicting the result.

Try it
class Book:
    def __init__(self, title, author): self.title, self.author = title, author
    def __str__(self): return f"{self.title} by {self.author}"
book = Book("Dune", "Frank Herbert")
print(book)

class BookCollection:
    def __init__(self): self.books = []
    def add_book(self, book): self.books.append(book)
    def __len__(self): return len(self.books)
collection = BookCollection()
for title in ("Dune", "The Hobbit", "Python Basics"): collection.add_book(title)
print(len(collection))

Run the example, then change one value and observe the result.

Python Playground

Loading Python runtime…

Edit the example and run it in your browser.

special_methods.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each operation before choosing an answer.

class Book:
    def __init__(self,title): self.title=title
    def __str__(self): return f"Book: {self.title}"
book=Book("Dune")
print(book)
print(str(book))
Choose the output

Practice independently

Mini Challenge — Describe an EBook

Override __str__() with super() and return a readable result.

Your goalOverride __str__() in EBook to return the title, author, and file size. Use super().__str__() for the basic book information, and return the final text instead of printing it.

ebook_str.py

Optional hints
  • Use super().__str__() for the base text.
  • Return a string; do not print it inside __str__().
Ask Sloby for a hint
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.