Python Glossary Русский

all Python 3pprintfunction

pprint.pprint()

printing

Prints a data structure with indentation and line breaks so that nested dicts and lists are easy to read.

Syntax

pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)

Dict keys are sorted (sort_dicts=True) — the order on screen is not the dict's real order; use pprint.pp() if that matters. Line breaks appear only when the representation is longer than width=80. depth=N hides levels deeper than N behind '...'. Your own classes print via __repr__ — without one you get <__main__.X object at 0x…>; dataclasses (3.10+) are laid out field by field.

Examples

import pprint
pprint.pprint({'b': 1, 'a': [1, 2]})  # → {'a': [1, 2], 'b': 1}
pprint.pprint(list(range(5)), width=10)  # → [0, / 1, / 2, / 3, / 4]
pprint.pprint([1, [2, [3, [4]]]], depth=2)  # → [1, [2, [...]]]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy