Python Quest

Track 11 · Lesson 4 of 12

String representations

Control how objects look when printed, inspected and put in f-strings with __str__ and __repr__.

About 30 minBeginner4 exercises

You will be able to

  • Explain why a plain object prints as <__main__.Parcel object at 0x…>
  • Write __str__ to give an object a readable, user-facing text form
  • Write __repr__ to give an unambiguous developer-facing form, following the ClassName(...) convention
  • Predict which representation print(), f-strings, the REPL and containers use

Included with a Formula Quest membership

It has 4 graded exercises with feedback, hints and solutions. Python Quest is included in Formula Quest memberships that cover Python; membership and billing are managed on Formula Quest.

View Formula Quest memberships

Tracks 1 and 2, the first lesson of every track, the playground and the dataset library are free.