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
Return the formatted representation of object. This takes into account the options passed to the PrettyPrinter constructor. ... Print the formatted representation of object on the configured stream, followed by a newline.
🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_module_pprint.asp
Python pprint Module
Pretty-print nested data in a readable ... "editor"]}]} pprint(data) Try it Yourself Β» Β· The pprint module formats Python objects in a way that's easier for humans to read....
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)) More on stackoverflow.com
🌐 stackoverflow.com
python - How to pretty print nested dictionaries? - Stack Overflow
This solution looks directly for object type, so you can pretty print almost everything, not only list or dict. Doesn't have any dependancy. Everything is put inside a string, so you can do whatever you want with it. The class and the function has been tested and works with Python 2.7 and 3.4. More on stackoverflow.com
🌐 stackoverflow.com
How to pretty print a BaseModel object?
As one possible alternative.... since the __repr__ of a model produces valid Python code, you can use black to print it nicely. The output is not consistent, unless you tweak the line length to be very low, but it gets the job done. import black def pprint(model: BaseModel): s = repr(model) pretty ... More on github.com
🌐 github.com
3
5
October 23, 2023
Pretty print JSON from URL
I've not done a lot in this space but something I have tried in the past with JSON's is to convert to a pandas DataFrame. I'm not sure if this is exactly what you are after, but the following stackoverflow post may help: https://stackoverflow.com/questions/21104592/json-to-pandas-dataframe More on reddit.com
🌐 r/learnpython
13
2
August 27, 2021
🌐
Python Module of the Week
pymotw.com β€Ί 2 β€Ί pprint
pprint – Pretty-print data structures - Python Module of the Week
It formats your object and writes it to the data stream passed as argument (or sys.stdout by default). from pprint import pprint from pprint_data import data print 'PRINT:' print data print print 'PPRINT:' pprint(data) $ python pprint_pprint.py PRINT: [(0, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (1, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (2, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'})] PPRINT: [(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'})]
🌐
Real Python
realpython.com β€Ί python-pretty-print
Prettify Your Data Structures With Pretty Print in Python – Real Python
October 4, 2022 - The folks at Python care about their pretty printer! They’ll probably have fixed this by the time you’re reading this tutorial. If underscore_numbers doesn’t work when you call pprint() directly and you really want pretty numbers, there is a workaround: When you create your own PrettyPrinter object, this parameter should work just like it does in the example above.
🌐
Real Python
realpython.com β€Ί lessons β€Ί pretty-print
Pretty Print (Video) – Real Python
In the previous lesson, I talked about how print() has changed between Python 2 and Python 3. In this lesson, I’ll be talking about the pprint() (pretty print) function out of the pprint (pretty print) library. In addition to the built-in print(…
Published Β  May 5, 2020
🌐
TutorialsPoint
tutorialspoint.com β€Ί python_text_processing β€Ί python_pretty_prints.htm
Python - Pretty Print Numbers
The python module pprint is used for giving proper printing formats to various data objects in python. Those data objects can represent a dictionary data type or even a data object containing the JSON data. In the below example we see how that data looks before applying the pprint module and ...
Find elsewhere
🌐
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 - To use pprint, begin by importing the library at the top of your Python file. ... From here you can either use the .pprint() method or instantiate your own pprint object with PrettyPrinter().
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί pprint-data-pretty-printer-python
pprint : Data pretty printer in Python - GeeksforGeeks
This article provides a quick way to pretty How to Print Dictionary in Python that has a dictionary as values. This is required many times nowadays with the advent of NoSQL databases.
Published Β  October 6, 2023
🌐
Python Engineer
python-engineer.com β€Ί posts β€Ί pprint-python
How to use pprint in Python? - Python Engineer
May 17, 2022 - [{'application': ['Data Science', ... and above. Returns True if the object passed is readable by pprint, if an object is readable it can be pretty printed....
🌐
Note.nkmk.me
note.nkmk.me β€Ί home β€Ί python
Pretty-print in Python: pprint | note.nkmk.me
May 17, 2023 - In Python, you can pretty-print objects such as lists (list) and dictionaries (dict) with the pprint module. pprint β€” Data pretty printer β€” Python 3.11.3 documentation Basic usage of pprint Specify ...
🌐
TestDriven.io
testdriven.io β€Ί tips β€Ί cce7ddc9-81be-4851-8287-199ba65378a5
Tips and Tricks - Python pprint – pretty-print data structures | TestDriven.io
You can use pprint.pprint to print a formatted representation of an object. ... import json import pprint from urllib.request import urlopen with urlopen("https://pypi.org/pypi/flask/json") as resp: project_info = json.load(resp)["info"] ...
🌐
Read the Docs
python.readthedocs.io β€Ί en β€Ί latest β€Ί library β€Ί pprint.html
8.11. pprint β€” Data pretty printer
January 16, 2022 - Return the formatted representation of object. This takes into account the options passed to the PrettyPrinter constructor. ... Print the formatted representation of object on the configured stream, followed by a newline.
🌐
Jython
jython.org β€Ί jython-old-sites β€Ί docs β€Ί library β€Ί pprint.html
8.18. pprint β€” Data pretty printer β€” Jython v2.5.2 documentation
Return the formatted representation of object. This takes into account the options passed to the PrettyPrinter constructor. ... Print the formatted representation of object on the configured stream, followed by a newline.
🌐
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 - PPrint stands for "Pretty Print", and is one of the native Python libraries that allows to customize outputs with its numerous parameters
🌐
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 - The key value prints based on their order insertion if it is set to False. Its default value is True. ... For example, if you want to work with JSON data from an API, or parsing log files, or debug Python objects with lots of levels, pprint should be your first choice that will save time. Firstly, you need to import the pprint module. ... You can use the pprint() method or instantiate your pprint object with PrettyPrinter().
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - We will also cover best practices ... Python, "pretty print" refers to formatting and presenting data structures such as lists, dictionaries, and tuples in a more readable and organized way....