Overloading String Representation

Mon 10 August 2026
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def __str__(self):
        return f"{self.name} costs ${self.price}"

p = Product("Laptop", 1200)
print(p)  # Laptop costs $1200
Laptop costs $1200


Score: 10

Category: chapter5_operator_overloading