pprint

The Python pprint module provides the capability to pretty-print data structures in a readable and easy-to-understand way. This module is particularly useful for displaying complex data structures in a human-friendly format and for visualizing nested lists, dictionaries, and other objects.

Here’s a quick example:

Python
>>> import pprint
>>> data = [
...     {"name": "Alice", "age": 30, "job": "Engineer"},
...     {"name": "Bob", "age": 25, "job": "Writer"}
... ]

>>> pprint.pprint(data)
[{'age': 30, 'job': 'Engineer', 'name': 'Alice'},
 {'age': 25, 'job': 'Writer', 'name': 'Bob'}]

Key Features

  • Formats data structures to be more readable
  • Supports nested data structures
  • Configurable depth and width for output

Frequently Used Classes and Functions

Object Type Description
pprint.pprint() Function Prints a formatted representation of an object
pprint.PrettyPrinter Class Provides advanced pretty-printing options
pprint.pformat() Function Returns a formatted representation of an object as a string

Examples

Use pprint.pformat() to get a pretty-printed string:

Python
>>> from pprint import pformat

>>> data = [
...     {"name": "Alice", "age": 30, "job": "Engineer"},
...     {"name": "Bob", "age": 25, "job": "Writer"}
... ]

>>> formatted_data = pformat(data)
>>> print(formatted_data)
[{'age': 30, 'job': 'Engineer', 'name': 'Alice'},
 {'age': 25, 'job': 'Writer', 'name': 'Bob'}]

Customize output with PrettyPrinter:

Python
>>> from pprint import PrettyPrinter
>>> pp = PrettyPrinter(indent=4)
>>> pp.pprint(data)
[   {'age': 30, 'job': 'Engineer', 'name': 'Alice'},
    {'age': 25, 'job': 'Writer', 'name': 'Bob'}]

Common Use Cases

  • Displaying complex data structures for debugging purposes
  • Logging structured data in a readable format
  • Formatting output for reports or presentations

Real-World Example

Suppose you have a deeply nested dictionary and you want to log it in a readable format for debugging:

Python
>>> import pprint

>>> config = {
...     "version": 1,
...     "settings": {
...         "logging": {
...             "level": "debug",
...             "handlers": ["console", "file"]
...         },
...         "database": {
...             "host": "localhost",
...             "port": 5432
...         }
...     }
... }

>>> pprint.pprint(config)
{'settings': {'database': {'host': 'localhost', 'port': 5432},
              'logging': {'handlers': ['console', 'file'], 'level': 'debug'}},
 'version': 1}

In this example, pprint() displays a nested configuration dictionary in a structured and readable format, making it easier to understand the configuration details at a glance.

Tutorial

Prettify Your Data Structures With Pretty Print in Python

The pprint module, Python's data pretty printer, is a useful part of the standard library. You can work with it for debugging data structures and increasing the readability of your output. In this tutorial, you'll find that pprint is both straightforward to use and also highly customizable.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated July 16, 2025