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:
>>> 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:
>>> 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
:
>>> 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:
>>> 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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Your Guide to the Python print() Function (Tutorial)
- The Python print() Function: Go Beyond the Basics (Course)
- The Python print() Function (Quiz)
- The Python print() Function (Quiz)
By Leodanis Pozo Ramos • Updated July 16, 2025