Python Glossary Русский

Python 3.8+pprintfunction

pprint.pp()

printing

Prints an object with indentation and line breaks, keeping dict key order; a handy replacement for print() with nested data.

Syntax

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

It differs from pprint.pprint() in one thing: sort_dicts=False, so dicts are shown in insertion order, as print() shows them. Line breaks appear only for long structures; a short one stays on one line — lower width to see the nesting. It writes to sys.stdout and returns None; pformat() gives a string.

Examples

import pprint
pprint.pp({'b': 1, 'a': [1, 2]}, width=10)
print(pprint.pformat({'b': 1, 'a': 2}, sort_dicts=False))  # → {'b': 1, 'a': 2}

See also

Open in the full glossarydocs.python.orgReport an inaccuracy