Special Methods Exercise
Question 1 of 5
What is the difference between __str__() and __repr__() methods?
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def __str__(self):
return f"{self.title} by {self.author}"
def __repr__(self):
return f"Book('{self.title}', '{self.author}')"str is faster, repr is more detailed
str provides user-friendly output, repr provides developer-oriented debugging output
str is for developers, repr is for users
Both methods serve the same purpose and are interchangeable