Last modified: Feb 10, 2026 By Alexander Williams

Python Print Object Guide for Beginners

Printing objects is a core skill in Python. It helps you debug and understand your code.

Beginners often see unhelpful output like <__main__.Car object at 0x10b8f7d90>. This guide will fix that.

You will learn to control how your objects are displayed. This makes your programs clearer and easier to maintain.

Why Printing Objects Can Be Confusing

When you create a custom class, Python does not know how to print it. It uses a default method.

This default shows the object's class name and memory address. This information is not useful for debugging.

Your goal is to override this default behavior. You need to tell Python what information is important.

The Two String Methods: __str__ vs __repr__

Python uses two special methods for object representation. Understanding the difference is key.

The __str__ method is for the end-user. It should be readable and nice.

The __repr__ method is for the developer. It should be unambiguous and detailed.

Often, __repr__ output can be used to recreate the object. The print() function uses __str__ first.

Example: A Simple Book Class

Let's create a class to see the problem and solution.


# A simple Book class without custom string methods
class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

# Create an instance
my_book = Book("The Python Guide", "Jane Coder", 350)

# Try to print it
print(my_book)

<__main__.Book object at 0x7f8a1c456550>

The output is not helpful. We cannot see the title or author. Let's fix it.

Implementing the __str__ Method

The __str__ method must return a string. This string is used by print() and str().

We will add it to our Book class.


class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

    # User-friendly representation
    def __str__(self):
        return f"'{self.title}' by {self.author} ({self.pages} pages)"

my_book = Book("The Python Guide", "Jane Coder", 350)
print(my_book)  # This now calls __str__
print(str(my_book)) # Explicit call to str()

'The Python Guide' by Jane Coder (350 pages)
'The Python Guide' by Jane Coder (350 pages)

Much better! The output is now informative for a user. For a deeper understanding of how objects store this data, see our Python Object Attributes Guide for Beginners.

Implementing the __repr__ Method

The __repr__ method is for developers. It is used by the REPL and the repr() function.

A good __repr__ often looks like a valid Python constructor call.


class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

    def __str__(self):
        return f"'{self.title}' by {self.author} ({self.pages} pages)"

    # Developer-friendly, unambiguous representation
    def __repr__(self):
        return f"Book(title='{self.title}', author='{self.author}', pages={self.pages})"

my_book = Book("The Python Guide", "Jane Coder", 350)

# Using print() calls __str__
print("Using print():", my_book)

# Using repr() calls __repr__
print("\nUsing repr():", repr(my_book))

# Just typing the object name in a console would show __repr__

Using print(): 'The Python Guide' by Jane Coder (350 pages)

Using repr(): Book(title='The Python Guide', author='Jane Coder', pages=350)

Now we have two clear representations. The __repr__ is perfect for logging and debugging. If your objects become more complex through inheritance, these methods are inherited and can be overridden.

Printing Objects in Collections

What happens when your object is inside a list or dictionary?

Python uses the __repr__ method for elements in a collection. This is why a good __repr__ is essential.


library = [
    Book("The Python Guide", "Jane Coder", 350),
    Book("Data Science Basics", "Alex Analyst", 500)
]

print(library)

[Book(title='The Python Guide', author='Jane Coder', pages=350), Book(title='Data Science Basics', author='Alex Analyst', pages=500)]

Because we defined __repr__, the list output is useful. Without it, we would just see memory addresses. This is especially important when dealing with nested data structures.

Using the pprint Module for Complex Objects

The built-in pprint (pretty print) module helps with complex, nested objects.

It formats output to be more readable. It also uses the object's __repr__ method.


import pprint

complex_data = {
    'shelf_1': library,
    'metadata': {'location': 'Main Hall', 'items': 2}
}

print("Standard print:")
print(complex_data)

print("\n\nUsing pprint:")
pprint.pprint(complex_data, indent=2)

Standard print:
{'shelf_1': [Book(title='The Python Guide', author='Jane Coder', pages=350), Book(title='Data Science Basics', author='Alex Analyst', pages=500)], 'metadata': {'location': 'Main Hall', 'items': 2}}

Using pprint:
{ 'metadata': {'items': 2, 'location': 'Main Hall'},
  'shelf_1': [ Book(title='The Python Guide', author='Jane Coder', pages=350),
               Book(title='Data Science Basics', author='Alex Analyst', pages=500)]}

The pprint output is much easier to read for deep structures.

Common Pitfalls and Best Practices

Here are some key tips for printing objects effectively.

Always define __repr__. It is your debugging safety net. If you can only define one method, make it __repr__.

Make __repr__ unambiguous. Ideally, the string should allow you to recreate the object.

Use __str__ for user-facing output. Keep it simple, friendly, and concise.

Keep methods fast. Do not perform heavy computations or I/O operations inside __str__ or __repr__.

For a full foundation on creating and using objects, our Python Objects: Classes, Instances, and Methods Guide is an excellent resource.

Conclusion

Printing objects in Python is simple once you know the rules. The default output is not an error. It is a request for more information.

Define the __str__ method for clear, user-friendly messages. Define the __repr__ method for detailed, developer-focused debugging.

Using these methods transforms your debugging experience. Your logs will be clear. Your console output will be helpful. Your code will be more professional.

Start by adding a basic __repr__ to your classes. You will see the benefit immediately.