──────────────────────────────────────

31. Festival Room Booking · Give a Booking a Readable Label

Use __str__ to give a booking an informative display without changing the booking.

Lesson files · lessons/composition/readable-object-labels

Before you start
- Store a room on a booking and read the room’s attributes.
- Build a sentence with an f-string and return it from a function.

By the end
- Return a label from Booking.__str__ using the group, room name and group size.
- Explain how a report can use the label before it prints anything.

Where you can use it
Turn each booking into a line a volunteer can copy into the festival schedule.

Python looks for a special method named __str__ when you pass an object to str() or print it. The double underscores mark a name Python recognises. Return a string from __str__. That lets the caller print the text, store it or test it. A booking label can read attributes from both the booking and its room.

Predict
What does label hold once str(room) has run?
- The text Hall (20 seats).
- The Room object.

Worked example (example.py)

class Room:
    def __init__(self, name, capacity):
        self.name = name
        self.capacity = capacity

    def __str__(self):
        return f"{self.name} ({self.capacity} seats)"


room = Room("Hall", 20)
label = str(room)
print(label)


Example result
Hall (20 seats)

Complete Booking.__str__ to return a label in the form Science Club booked Hall for 8 people. Use the group, room name and group size from the booking. Keep the final period.

Starter program (starter.py)

class Room:
    def __init__(self, name, capacity):
        self.name = name
        self.capacity = capacity


class Booking:
    def __init__(self, room, group, people):
        self.room = room
        self.group = group
        self.people = people

    def __str__(self):
        return self.group


group = input()
room = Room(input(), 20)
people = int(input())
print(Booking(room, group, people))


Starter input
Science Club
Hall
8

Hint
Use an f-string. Read the room’s name with self.room.name.

Case 1. Try the sample
Input
Science Club
Hall
8
Expected output
Science Club booked Hall for 8 people.

Case 2. Change all fields
Input
Art Club
Studio
3
Expected output
Art Club booked Studio for 3 people.

Case 3. Room name with spaces
Input
Robotics
East Hall
12
Expected output
Robotics booked East Hall for 12 people.

Stretch
Call str(booking) twice and assert that the label matches both times.

Reflect
Why might a report call str(booking) without printing yet?

Check your prediction
The text Hall (20 seats). Python calls the room’s __str__ and uses the string it returns.
