What is the difference between __str__ and __repr__?

Python classes and OOP · Freshers

Model answer

__str__ gives a readable text for users (used by print and str()). __repr__ gives an unambiguous developer representation, ideally one that could recreate the object, and is used in the REPL and logs. If only __repr__ is defined, it is also used for str.

Learn this topic

Classes and Objects in Python: A Gentle Introduction in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions