Q46: What are the differences between __str__ and __repr__?

  • __str__(): Readable string representation intended for end-users (str(obj) / print(obj)).
  • __repr__(): Unambiguous string representation intended for developers/debugging (repr(obj)). Ideally returns valid Python code that could recreate the object.
Key Takeaways
  • __str__ = User-friendly formatting. __repr__ = Unambiguous developer representation.
Knowledge Check

Which method is invoked by container fallback when printing a list of custom objects print([obj1, obj2])?