Python __str__ Method

Complete guide to Python's __str__ method covering string representation, printing objects, and custom string conversions.

Python __str__ Method

Python str Method

Last modified April 8, 2025

This comprehensive guide explores Python’s str method, the special method responsible for string representation of objects. We’ll cover basic usage, formatting, differences with repr, and examples.

Basic Definitions

The str method returns a string representation of an object. It’s called by the str() built-in function and by the print() function to display objects in a human-readable format.

Key characteristics: it must return a string object, is meant to be readable, and should provide a concise representation. It’s different from repr which aims to be unambiguous and complete.

Basic str Implementation

Here’s a simple class implementing str to provide a custom string representation. This demonstrates the fundamental usage pattern.

basic_str.py

class Person: def init(self, name, age): self.name = name self.age = age

def __str__(self):
    return f"Person: {self.name}, {self.age} years old"

p = Person(“Alice”, 30) print(p) # Calls str implicitly print(str(p)) # Explicit call

This example shows how str provides a readable string format. When we print the object or call str() on it, Python invokes str to get the string representation.

The output will be “Person: Alice, 30 years old” in both cases, demonstrating how str customizes the display of our object.

str vs repr

This example demonstrates the difference between str and repr, showing when each is called and their intended purposes.

str_vs_repr.py

class Point: def init(self, x, y): self.x = x self.y = y

def __repr__(self):
    return f"Point({self.x}, {self.y})"

def __str__(self):
    return f"({self.x}, {self.y})"

p = Point(3, 4) print(p) # Uses str: (3, 4) print(repr(p)) # Uses repr: Point(3, 4)

repr aims to be unambiguous (good for debugging), while str aims to be readable. The print() function uses str, while the REPL uses repr.

If str isn’t defined, Python falls back to repr. It’s good practice to implement both for clarity in different contexts.

Formatting with str

The str method can include complex formatting to present object data in a visually appealing way, as shown in this table-like representation.

formatting_str.py

class Product: def init(self, name, price, quantity): self.name = name self.price = price self.quantity = quantity

def __str__(self):
    header = f"{'Name':<15}{'Price':>10}{'Quantity':>12}"
    divider = "-" * 37
    row = f"{self.name:<15}${self.price:>9.2f}{self.quantity:>12}"
    return f"{header}\n{divider}\n{row}"

item = Product(“Laptop”, 1299.99, 5) print(item)

This implementation creates a formatted table-like output with aligned columns. The string formatting mini-language is used to control spacing and decimal places for professional-looking output.

The output will show a header row, divider line, and product data neatly aligned, demonstrating how str can enhance readability.

str in Inheritance

When using inheritance, str can be extended to include information from both parent and child classes, as shown in this example.

inheritance_str.py

class Animal: def init(self, species): self.species = species

def __str__(self):
    return f"Species: {self.species}"

class Dog(Animal): def init(self, name, breed): super().init(“Canine”) self.name = name self.breed = breed

def __str__(self):
    return f"{super().__str__()}, Name: {self.name}, Breed: {self.breed}"

dog = Dog(“Buddy”, “Golden Retriever”) print(dog)

The child class’s str calls the parent’s version using super(), then adds its own information. This creates a complete string representation combining both levels of the class hierarchy.

The output will be “Species: Canine, Name: Buddy, Breed: Golden Retriever”, showing how inheritance chains can build comprehensive string representations.

Dynamic str Content

str can generate dynamic content based on the object’s current state, as demonstrated in this time-based greeting example.

dynamic_str.py

import datetime

class Greeter: def init(self, name): self.name = name

def __str__(self):
    hour = datetime.datetime.now().hour
    if hour < 12:
        time_of_day = "morning"
    elif hour < 18:
        time_of_day = "afternoon"
    else:
        time_of_day = "evening"
    return f"Good {time_of_day}, {self.name}!"

greeting = Greeter(“Alice”) print(greeting) # Changes based on current time

This str implementation checks the current time and generates an appropriate greeting message. The string representation changes dynamically based on when it’s called.

This demonstrates how str can incorporate runtime information to create context-aware representations, making objects more interactive.

Best Practices

  • Keep it readable: str should prioritize human readability

  • Include key information: Focus on most important attributes

  • Return a string: Must return a string object, not print

  • Consider performance: Complex formatting may impact performance

  • Implement repr too: Always provide both string methods

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.

ad ad