From the course: Advanced Python: Object-Oriented Programming
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
String representations - Python Tutorial
From the course: Advanced Python: Object-Oriented Programming
String representations
- [Instructor] Let's talk about string representation. Every language or almost every language has a way of saying, I want this value to display this way as a string. In Java and C#, it's toString. In JavaScript, it's string. In Ruby, it's to_s or to_str. And in Python, we actually have three ways of doing it. The first one is dunder repr. This is representation for developer, usually a way to create such a value. The second one is str, dunder str, and this is for display for users. And we also have dunder format which is more specialized and have more options than the previous two. So let's have a look. Let's say we have a payment, which has an amount and a currency, and we define all methods. We have dunder str, which is for display, so we display the currency and we display the amount with only two decimal points. And then we have the repr, which is for representation to developers. And we take the class name, and the class name can be also from one of the class inheritance, so we…