__str__
Dunder MethodPython 2.0+Beginner
Returns a human-readable string representation (used by print and str())
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
class Product: def __init__(self, name, price): self.name = name self.price = price def __str__(self): return f"{self.name}: ${self.price:.2f}" p = Product("Widget", 9.99) print(p) print(str(p))
__str__ returns a human-readable string representation (used by print and str()). Implementing it lets you customize how Python interacts with your objects.
Try in PlaygroundTags
oopmagic-methodprotocolcore