Special Method Protocols

__str__() — object lifecycle protocol

Provide readable text for str() and print(). If absent, object.__str__ uses repr(). The hook must return str.

Formal Syntax & Structure

python syntaxpython
def __str__(self):

Return Value

str

Readable representation.

Working Example

Example Snippetpython
class Ticket:    def __str__(self): return "ticket 7"    def __repr__(self): return "Ticket(7)"print(str(Ticket()))
Python Reference