OnlinePython

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}')"