Python Glossary Русский

all Python 3pprintterm

pprint

module

Pretty-printing of nested data structures: line breaks, indentation and sorted dictionary keys.

Syntax

import pprint

pprint is for when print dumps a long nested structure as one unreadable line: it wraps it to the given width and aligns it with indentation. Note that pprint sorts dictionary keys by default; pp or sort_dicts=False keep insertion order. A short structure that fits the width is printed on one line, just like print.

Examples

import pprint
data = {'b': [1, 2], 'a': {'x': 1}}
pprint.pprint(data)  # → {'a': {'x': 1}, 'b': [1, 2]}
pprint.pp(data)  # → {'b': [1, 2], 'a': {'x': 1}}
print(pprint.pformat(list(range(12)), width=20).count('\n'))  # → 11

See also

Open in the full glossarydocs.python.orgReport an inaccuracy