Dan's solution is just wrong, and Ismail's in incomplete.

  1. __str__() is not called, __repr__() is called.
  2. __repr__() should return a string, as pformat does.
  3. print normally indents only 1 character and tries to save lines. If you are trying to figure out structure, set the width low and indent high.

Here is an example

class S:
    def __repr__(self):
        from pprint import pformat
        return pformat(vars(self), indent=4, width=1)

a = S()
a.b = 'bee'
a.c = {'cats': ['blacky', 'tiger'], 'dogs': ['rex', 'king'] }
a.d = S()
a.d.more_c = a.c

print(a)

This prints

{   'b': 'bee',
    'c': {   'cats': [   'blacky',
                         'tiger'],
             'dogs': [   'rex',
                         'king']},
    'd': {   'more_c': {   'cats': [   'blacky',
                               'tiger'],
                  'dogs': [   'rex',
                              'king']}}}

Which is not perfect, but passable.

Answer from Charles Merriam on Stack Overflow
🌐
Python
docs.python.org › 3 › library › pprint.html
pprint — Data pretty printer
Source code: Lib/pprint.py The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted struc...
🌐
Analytics Vidhya
analyticsvidhya.com › home › how to use pprint in python? [explained with examples]
How to Use PPrint in Python? [Explained with Examples]
October 19, 2024 - The library contains just a single class, called pprint(). There are in total six parameters that can be used with this class.
Discussions

python - How to use pprint to print an object using the built-in __str__(self) method? - Stack Overflow
I have a Python script which processes a .txt file which contains report usage information. I'd like to find a way to cleanly print the attributes of an object using pprint's pprint(vars(object)) function. The script reads the file and creates instances of a Report class. More on stackoverflow.com
🌐 stackoverflow.com
Print dataclasses with indendation levels?
You can try prettyprint, but there is no way to tell without an example. https://docs.python.org/3/library/pprint.html More on reddit.com
🌐 r/learnpython
7
1
January 23, 2024
New Protocol for Pretty Printing
Looks pretty cool to me. dataclasses have this kind of repr thing built in, but it would be nice to have this tool if i were doing something else, to build them on my own. that said, your code above requires putting in the defaults twice. i bet you could automate the work inside __rich_repr__ by using inspect.signature, something like this: from inspect import signature from rich.repr import rich_repr @rich_repr class Bird: def __init__(self, name, eats=None, fly=True, extinct=False): self.name = name self.eats = list(eats) if eats else [] self.fly = fly self.extinct = extinct def __rich_repr__(self): for name, param in signature(Bird).parameters.items(): if param.default == param.empty: yield name, getattr(self, name) else: yield name, getattr(self, name), param.default if __name__ == "__main__": print(Bird("gull", eats=["fish", "chips", "ice cream", "sausage rolls"])) print(Bird("penguin", eats=["fish"], fly=False)) print(Bird("dodo", eats=["fruit"], fly=False, extinct=True)) More on reddit.com
🌐 r/Python
2
66
June 11, 2021
Made a color pretty printer which auto-generates a label before the value.
Check out https://github.com/samuelcolvin/python-devtools More on reddit.com
🌐 r/Python
1
8
January 19, 2024
🌐
Python Module of the Week
pymotw.com › 2 › pprint
pprint – Pretty-print data structures - Python Module of the Week
$ python pprint_pformat.py DEBUG Logging pformatted data DEBUG [(0, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H'}), (1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H'}), (2, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H'})] The PrettyPrinter class used by pprint() can also work with your own classes, if they define a __repr__() method.
🌐
Real Python
realpython.com › python-pretty-print
Prettify Your Data Structures With Pretty Print in Python – Real Python
March 18, 2026 - The output isn’t pretty when you use print() on large dictionaries or long lists—it’s efficient, but not pretty. The pprint module in Python is a utility module that you can use to print data structures in a readable, pretty way.
🌐
Jython
jython.org › jython-old-sites › docs › library › pprint.html
8.18. pprint — Data pretty printer — Jython v2.5.2 documentation
The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable.
🌐
W3Schools
w3schools.com › python › ref_module_pprint.asp
Python pprint Module
The pprint module formats Python objects in a way that's easier for humans to read.
🌐
Python
docs.python.org › 2 › library › pprint.html
8.18. pprint — Data pretty printer — Python 2.7.18 documentation
July 19, 2020 - The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › python pprint module
Python Pprint Module - Scaler Topics
May 4, 2023 - Now, you can use the function pprint.pprint() to “pretty-print” your data. You can also create an object of the class PrettyPrinter() and customize it according to your requirements.
🌐
Python Engineer
python-engineer.com › posts › pprint-python
How to use pprint in Python? - Python Engineer
May 17, 2022 - This is the most famous function from the pprint module. It passes the arguments and keyword arguments to the PrettyPrinter class constructor.
🌐
Read the Docs
stackless.readthedocs.io › en › 2.7-slp › library › pprint.html
8.18. pprint — Data pretty printer — Stackless-Python 2.7.15 documentation
>>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] >>> stuff.insert(0, stuff[:]) >>> pp = pprint.PrettyPrinter(indent=4) >>> pp.pprint(stuff) [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'], 'spam', 'eggs', 'lumberjack', 'knights', 'ni'] >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ... ('parrot', ('fresh fruit',)))))))) >>> pp = pprint.PrettyPrinter(depth=6) >>> pp.pprint(tup) ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...))))))) The PrettyPrinter class supports several derivative functions:
🌐
DataFlair
data-flair.training › blogs › python-pprint
Python pprint Module - Python Data Pretty Printer - DataFlair
April 24, 2026 - Last, we talked about the repr() built-in function in Python. In that, we observed the __repr__() method. Today, let’s see how we can make pprint work with our own classes by making use of the __repr__() method.
🌐
seenode blog
seenode.com › blog › how-to-use-python-pretty-print-pprint
How to Use Python Pretty Print (pprint) for Cleaner Python Output | seenode blog
May 19, 2025 - It refers to “Pretty Print,” the native Python library that allows developers to customize their output experience with its different parameters and flags for a single class ...
🌐
GeeksforGeeks
geeksforgeeks.org › pprint-data-pretty-printer-python
pprint : Data pretty printer in Python - GeeksforGeeks
This article is about a pretty useful built-in module in Python, pprint. The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a well-formatted and more readable way!
Published: October 6, 2023
🌐
Better Programming
betterprogramming.pub › how-to-pretty-print-in-python-9b1d8764d151
How To Pretty Print in Python. Start using the pprint library | by Jonathan Hsu | Better Programming
December 5, 2019 - Short for Pretty Printer, pprint is a native Python library that allows you to customize the formatting of your output. Refer to the official documentation for a full list of library and all properties available.
🌐
Python
docs.python.org › 3.4 › library › pprint.html
8.11. pprint — Data pretty printer — Python 3.4.10 documentation
The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable.
🌐
TutorialsPoint
tutorialspoint.com › python_text_processing › python_pretty_prints.htm
Python Text Processing - Pretty Printing
Those data objects can represent ... module and after applying it. import pprint student_dict = {'Name': 'Tusar', 'Class': 'XII', ......
🌐
GitHub
github.com › python › cpython › blob › main › Lib › pprint.py
cpython/Lib/pprint.py at main · python/cpython
· Functions · --------- · pformat() Format a Python object into a pretty-printed representation. · pprint() Pretty-print a Python object to a stream [default is sys.stdout]. ·
Author: python
🌐
CodersLegacy
coderslegacy.com › home › python › python pprint (pretty print)
Python pprint (pretty print) - CodersLegacy
May 24, 2020 - pprint is a Python library used to display complex data structures in a more userfriendly and readable format than the standard print() function.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pretty-print in Python: pprint | note.nkmk.me
May 17, 2023 - s_normal = str(l) print(s_normal) # [{'Name': 'Alice XXX', 'Age': 40, 'Points': [80, 20]}, {'Name': 'Bob YYY', 'Age': 20, 'Points': [90, 10]}, {'Name': 'Charlie ZZZ', 'Age': 30, 'Points': [70, 30]}] print(type(s_normal)) # <class 'str'> ... You can use pprint.pformat() to get the output of pprint.pprint() as the string str.