liveBook · Manning

__str__

__str__ is a Python special method that returns an object's informal, human-readable string representation.

How 3 Manning books explain it

Python How-To

__str__ is a Python special method that returns an informal, user-oriented string representation of an object.

How it is used
Uses __str__ to make custom objects and enumeration members readable in user-facing output.
How deep it goes
Explains how to override the instance method, what it must return, and how printing, interpolation, and explicit conversion invoke it.
Examples
Demonstrates a task displaying its attributes and a direction enumeration displaying a lowercase name.
What you can do after
Apply it by overriding __str__ to produce descriptive output for custom instances.

Read how Python How-To explains __str__ →

Python Workout: 50 ten-minute exercises

A Python special method that returns an object's user-oriented string representation.

How it is used
Explains how objects can provide readable user-facing representations alongside developer-oriented representations.
How deep it goes
Defines the audience distinction and shows a Bowl example, but defers deeper object-model mechanics.
Examples
Uses Bowl and Scoop objects, numeric conversion with str(5), and joining converted ranges.
What you can do after
Apply it via Python string conversion and object representation choices for readable output.

Read how Python Workout: 50 ten-minute exercises explains __str__ →

The Quick Python Book, Fourth Edition

__str__ is Python's special method for producing an informal string representation of an object.

How it is used
Uses __str__ to make custom objects readable during printing, debugging, and conversion to user-facing strings.
How deep it goes
Explains automatic invocation and demonstrates formatting and recursive delegation, without covering lower-level implementation details.
Examples
Demonstrates RGB Color formatting, printed Circle descriptions, and recursive HTML document rendering.
What you can do after
Apply it via print() and str() to produce informative representations of custom class instances.

Read how The Quick Python Book, Fourth Edition explains __str__ →

Also covered in

What to know first

Related concepts

Questions about __str__

What is __str__?

__str__ is a Python special method that returns an object's informal, human-readable string representation.

Which Manning books explain __str__?

Python How-To, Python Workout: 50 ten-minute exercises and The Quick Python Book, Fourth Edition.

What should I know before learning __str__?

Manning books that teach __str__ usually introduce constructor method, string, inheritance and object first.

What is the difference between __str__ and __repr__?

__str__ is a Python special method that returns an object's informal, human-readable string representation. __repr__ is a Python special method that returns an object’s developer-oriented string representation, ideally unambiguous and suitable for recreating an equivalent object.

What is __str__ a kind of?

__str__ is a kind of special method. A special method is a Python method with a conventionally reserved name that defines an object's behavior for language operations or built-in functions.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.