AI engineering glossary · Python advanced

What is __str__?

__str__: The special method returning a readable text form of an object for end users. It is used by print() and str(). If it is missing, Python falls back to __repr__.

Level: intermediateTopic: Python advanced

__str__: an example

def __str__(self): return f'{self.name} (Rs {self.price})' prints a product as 'Pen (Rs 10)'.

Related terms

More Python advanced terms

Where you learn it

__str__ is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.