__str__
__str__ is a Python special method that returns an object's informal, human-readable string representation.
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.
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
- constructor methodA constructor method is a type-associated operation involved in creating an instance and establishing its initial state.
- stringA string is a programming-language value representing textual data as an ordered sequence of characters or encoded units.
- inheritanceInheritance is a mechanism in which a derived type or descendant element receives properties or behavior from a parent, subject to rules that may allow specialization or overriding.
- objectAn object is a distinct entity represented or manipulated within a computational system, typically as a value, data structure, resource, or instance of a type.
Related concepts
- printused by · Print is a programming operation that writes a value or its textual representation to an output destination, often standard output.
- __repr__contrasts with · __repr__ is a Python special method that returns an object’s developer-oriented string representation, ideally unambiguous and suitable for recreating an equivalent object.
- special methodkind of · 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.
- stringproduces · A string is a programming-language value representing textual data as an ordered sequence of characters or encoded units.
- comprehensionused by · A comprehension is a language construct that creates a collection by iterating over one or more sources, optionally filtering elements and transforming them.
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.


