Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Standard Library: Data Structures and Functional Tools

The pprint module

Reference for Python's pprint module: pprint(), pformat(), width, depth, indent, compact, sort_dicts and underscore_numbers for readable nested data.

The pprint module ("pretty print") formats nested data structures so a human can read them. Plain print() of a large dictionary produces one very long line; pprint breaks it over several lines, indents nested levels and wraps at a configurable width. It is a quick debugging aid for parsed JSON, configuration dictionaries and API responses.

The output is still valid Python for the basic types, so you can copy it into a file. pprint() writes to a stream (standard output by default); pformat() returns the same text as a string, which is better for logging.

Python
from pprint import pprint

pprint({"name": "Ada", "langs": ["Python", "C"], "meta": {"born": 1815}})

Try it

Python Wrapping and sorting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python depth, compact, indent and custom reprs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
pprint.pprint(object, stream=None, indent=1, width=80, depth=None,
              *, compact=False, sort_dicts=True, underscore_numbers=False)
pprint.pformat(object, indent=1, width=80, depth=None,
               *, compact=False, sort_dicts=True, underscore_numbers=False)
Name Description
object The value to format.
stream File-like object to write to; defaults to sys.stdout.
indent Spaces of indentation added for each nesting level.
width Desired maximum line width. Items that cannot be split (long strings, for example) may exceed it.
depth Maximum nesting levels to show; deeper levels print as ... inside brackets ([...]). None means no limit.
compact If True, put as many items on a line as fit; if False, one item per line once the container does not fit.
sort_dicts If True (the default), dictionary keys are sorted. Pass False for insertion order (3.8).
underscore_numbers If True, integers are shown with underscores as thousands separators, such as 1_234_567_890 (3.10).

Return value: pprint() returns None; pformat() returns the formatted str.

Other names: PrettyPrinter (a configurable class with pprint() and pformat()), pp() (like pprint() but with sort_dicts=False, 3.8), saferepr() (a repr() that survives cycles), isreadable() and isrecursive().

Usage notes

  • Sorting is on by default. That makes diffs stable but hides insertion order; since dictionaries preserve order, pprint.pp(data) or sort_dicts=False is often what you want.
  • Long strings are split at spaces. A string that does not fit becomes several adjacent literals in parentheses, as in the 'a long string value ' 'here' output above. Words longer than the width, numbers and other atoms are never broken. Use textwrap for prose.
  • Short structures stay on one line. If the whole value fits in width, pprint prints it exactly like repr().
  • It is for people, not machines. Use json.dumps(obj, indent=2) for JSON output, and repr() when you need a faithful one-line form.
  • Custom classes are shown with their repr(). Data classes get a multi-line layout when they are long (3.10+).
  • For logging, use pformat() and pass the string: logger.debug("state:\n%s", pformat(state)).
  • Recursive structures are safe. A reference to an enclosing container shows as <Recursion on list with id=...>; the id changes between runs, so avoid printing it in tests.