What is the difference between `__str__` and `__repr__` in Python classes?

Python Developer Hard

Python Developer — Hard

What is the difference between `__str__` and `__repr__` in Python classes?

Key points

  • __str__ for user-facing output, __repr__ for developer representation
  • __repr__ serves both purposes if __str__ is not defined
  • __str__ used by print() and str() functions

Ready to go further?

Related questions