Editor

Add __str__ to a Product

Intermediate · Write the code · from Python Classes and Objects: OOP from the Ground Up

From the lesson: Python Classes and Objects: OOP from the Ground Up

Imagine you own a bakery and you have a cookie cutter shaped like a star. Every time you press it into dough, you get a new star-shaped cookie. The cutter itself isn't a cookie - it's a blueprint for making cookies.

Read the lesson

Create a Product class with name and price attributes. Add a __str__ method that returns the format 'Product: <name> - $<price>'. Create a product with name 'Laptop' and price 999.99, then print it.

# Create your Product class here

More Classes and Objects exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz