On this page
pprint
Prev: posixpath | Up: stdlib_module | Next: profile
Rel: datetime, zoneinfo, calendar, collections, heapq
Ref: docs.python.org — pprint
Tags: #public #python3 #stdlib
Import
import pprint
Overview
Pretty-prints nested Python data structures (dict, list, tuple, set, dataclasses) with clean indentation, line wrapping, and optional depth truncation. Reach for pprint.pprint() or pprint.pformat() during debugging, CLI diagnostics, and readable test failure messages.
Quick Start
import pprint
data = {"users": [{"id": 1, "name": "Ada", "roles": ["admin", "dev"]}], "active": True}
formatted = pprint.pformat(data, width=40, sort_dicts=False)
assert "'name': 'Ada'" in formatted
Implementation Walkthrough
1. Controlling Width, Depth & Insertion Order
Pass sort_dicts=False (Python 3.8+) to preserve dictionary insertion order and depth= to prevent giant nested trees from flooding the terminal:
import pprint
nested = {"a": {"b": {"c": {"d": [1, 2, 3]}}}}
summary = pprint.pformat(nested, depth=2, sort_dicts=False)
assert summary == "{'a': {'b': {...}}}"
2. Reusable PrettyPrinter Instance & Logging Integration
Use pformat() when integrating with logging so output goes through your logger rather than printing directly to stdout:
import logging
import pprint
pp = pprint.PrettyPrinter(indent=2, width=80, compact=True, sort_dicts=False)
logging.getLogger(__name__).debug("State:\n%s", pp.pformat({"items": list(range(10))}))
Gotchas & Boundaries
sort_dicts=Trueis the default: Passsort_dicts=Falseif you want to inspect actual dictionary insertion order.- Output is Python
reprsyntax, not valid JSON: Single quotes,True/None, and sets cannot be parsed byjson.loads(); usejson.dumps(..., indent=2)when you need JSON. - Handles self-referential structures safely: Recursive lists/dicts render as
<Recursion on list with id=...>instead of crashing withRecursionError.
Common Functions
- pprint.isreadable() —
isreadable(object)— Determine if saferepr(object) is readable by eval(). - pprint.isrecursive() —
isrecursive(object)— Determine if object requires a recursive representation. - pprint.pformat() —
pformat(object, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)— Format a Python object into a pretty-printed representation. - pprint.pp() —
pp(object, *args, sort_dicts=False, **kwargs)— Pretty-print a Python object - pprint.pprint() —
pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)— Pretty-print a Python object to a stream [default is sys.stdout]. - pprint.saferepr() —
saferepr(object)— Version of repr() which can handle recursive data structures.
Classes & Objects
- pprint.PrettyPrinter —
pprint.PrettyPrinterclass. (Methods:.format(),.isreadable(),.isrecursive(),.pformat(),.pprint())
Linked from
Related notes
- tuts/python3/stdlib_module/posixpath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/profile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/calendar Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/collections Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/datetime Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/heapq Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/logging Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/pprint/isreadable Linked here; shared tags: #stdlib