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

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.