What is the difference between __str__ and __repr__ in Python?


In Python, the __str__ method is called when the object is printed as a string, such as when you use the print() function. The __repr__ method is called when the object is represented as a string, such as when you use the repr() function. The main difference between the two is that the __str__ method is meant to be more user-friendly, while the __repr__ method is meant to be more precise and contain all the information necessary to recreate the object exactly.

Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.


Subscribe to be the first to know when a new Python course is released.