The pprint module has a function named pformat, for just that purpose.

From the documentation:

Return the formatted representation of object as a string. indent, width and depth will be passed to the PrettyPrinter constructor as formatting parameters.

Example:

>>> import pprint
>>> people = [
...     {"first": "Brian", "last": "Kernighan"}, 
...     {"first": "Dennis", "last": "Richie"},
... ]
>>> pprint.pformat(people, indent=4)
"[   {   'first': 'Brian', 'last': 'Kernighan'},\n    {   'first': 'Dennis', 'last': 'Richie'}]"
Answer from SilentGhost on Stack Overflow
🌐
Python
docs.python.org › 3 › library › pprint.html
pprint — Data pretty printer
Return the formatted representation of object as a string. indent, width, depth, compact, sort_dicts and underscore_numbers are passed to the PrettyPrinter constructor as formatting parameters and their meanings are as described in the documentation above.
🌐
Real Python
realpython.com › python-pretty-print
Prettify Your Data Structures With Pretty Print in Python – Real Python
October 4, 2022 - pformat() is a tool you can use to get between the pretty printer and the output stream. Another use case for this might be if you’re building an API and want to send a pretty string representation of the JSON string. Your end users would probably appreciate it! Python’s pprint() is recursive, meaning it’ll pretty-print all the contents of a dictionary, all the contents of any child dictionaries, and so on.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pretty-print in Python: pprint | note.nkmk.me
May 17, 2023 - If the parameter sort_dicts, added ... the pretty-printed output to a string (str) instead of directly printing it, use pprint.pformat() ......
🌐
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
🌐
Finxter
blog.finxter.com › python-how-to-pprint-to-a-string-not-printing-to-shell
How to Pretty Print to a String Not to a Shell in Python? – Be on the Right Side of Change
You can pretty print it by importing the pprint module and call pprint(): import pprint my_dict = {'alice': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'bob': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'carl': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'dave': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} pprint.pprint(my_dict) ...
🌐
Rich
rich.readthedocs.io › en › stable › pretty.html
Pretty Printing — Rich 14.1.0 documentation
>>> pprint("Where there is a Will, there is a Way", max_string=21) Rich offers a Pretty class which you can use to insert pretty printed data in to another renderable.
Find elsewhere
🌐
Python Module of the Week
pymotw.com › 2 › pprint
pprint – Pretty-print data structures - Python Module of the Week
If you need to format a data structure, but do not want to write it directly to a stream (for logging purposes, for example) you can use pformat() to build a string representation that can then be passed to another function. import logging from pprint import pformat from pprint_data import data logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s', ) logging.debug('Logging pformatted data') logging.debug(pformat(data)) $ 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'})]
🌐
Real Python
realpython.com › lessons › pretty-print
Pretty Print (Video) – Real Python
If you pass in two strings, you will get an error, unlike in regular print(). 03:08 So you’re stuck with one object at a time here, or composing the object in its own str() call before you call pprint(). A lot of terminals allow you to control the font color and weight. 03:22 I’ll show you how to use ANSI escape sequences in the next lesson. Become a Member to join the conversation. ... Get a Python Cheat Sheet (PDF) and learn the basics of Python, like working with data types, dictionaries, lists, and Python functions:
Published   May 5, 2020
🌐
TutorialsPoint
tutorialspoint.com › python_text_processing › python_pretty_prints.htm
Python Text Processing - Pretty Printing
{'Address': {'FLAT ': 1308, 'LANE ': 2, 'CITY ': 'HYD', 'BLOCK ': 'A'}, 'Name': 'Tusar', 'Class': 'XII'} ***With Pretty Print*** ----------------------- {'Address': {'BLOCK ': 'A', 'CITY ': 'HYD', 'FLAT ': 1308, 'LANE ': 2}, 'Class': 'XII', 'Name': 'Tusar'}
🌐
Python Engineer
python-engineer.com › posts › pprint-python
How to use pprint in Python? - Python Engineer
May 17, 2022 - from pprint import pformat nested_dict ... and above. Returns True if the object passed is readable by pprint, if an object is readable it can be pretty printed....
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - Reconstruct your custom Python objects from a JSON string by using the object_hook parameter in json.loads() to intercept and transform the data. Improve the debugging experience by using the rich library to pretty-print JSON with syntax highlighting directly in your terminal.
🌐
Medium
medium.com › @blogshub4 › how-to-pretty-print-a-json-string-in-python-98a85f99ecb4
How to Pretty Print a JSON String in Python | by Blogshub | Medium
December 22, 2024 - Here’s an example of pretty-printing a minified JSON string: ... # Minified JSON string json_data = '{"name": "Dharmender", "age": 25, "city": "Bangalore"}'# Convert to Python object parsed_data = json.loads(json_data)# Pretty print with indentation pretty_json = json.dumps(parsed_data, indent=4) print(pretty_json)
🌐
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
PrettyPrinter.format(object, context, maxlevels, level)¶ · Returns three values: the formatted version of object as a string, a flag indicating whether the result is readable, and a flag indicating whether recursion was detected. The first argument is the object to be presented.
🌐
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().
🌐
Linux Hint
linuxhint.com › use-pretty-print-module-python
How to Use Pretty Print Module in Python – Linux Hint
The “pretty” variable stores the formatted string so that it can be used later in the code. The last statement prints the output of the pretty variable. After running the above code sample, you should get the following output: ... If you want to print a Python dictionary into a well indented JSON like structure, the pprint module might not be sufficient.
🌐
IPython
ipython.readthedocs.io › en › stable › api › generated › IPython.lib.pretty.html
Module: lib.pretty — IPython 9.11.0 documentation
The pretty library allows developers to add pretty printing rules for their own objects. This process is straightforward. All you have to do is to add a _repr_pretty_ method to your object and call the methods on the pretty printer passed:
🌐
Reddit
reddit.com › r/python › new protocol for pretty printing
r/Python on Reddit: New Protocol for Pretty Printing
June 10, 2021 -

I've recently documented a protocol in Rich to add pretty printing to arbitrary objects.

The problem was that containers like dicts, lists, sets etc could be formatted over multiple lines, but custom classes are limited to a string that can be generated from __repr__. The new protocol in Rich adds a __rich_repr__ method which allows an object to declare how it should be pretty printed.

Looking for feedback. Here are the docs.

Here's an example of a __rich_repr__ method.

And here's what it looks like when you pretty print a Bird instance: