From the course: Python Object-Oriented Programming

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

String representation

String representation

- [Instructor] The first set of magic methods we're going to learn about are the ones that Python uses to generate string representations of objects. And we saw a little peek of this in the prior chapter when we worked on object composition. Let's open up magic str start, and you can see that I have my book class defined with some properties along with a couple of statements to create book objects. There are two magic string functions, str and repr. The str function is used to provide a user-friendly string description of the object and is usually intended to be displayed to the user. The repr function is used to generate a more developer facing string that ideally can be used to recreate the object in its current state. It's also commonly used for debugging purposes so it gets used a lot to display detailed information. These functions get invoked on an object in a variety of ways. For example, when you call the print function…

Contents