pprint — Data pretty printer¶
Source code: Lib/pprint.py
The pprint module provides a capability to “pretty-print” arbitrary
Python data structures in a form which can be used as input to the interpreter.
If the formatted structures include objects which are not fundamental Python
types, the representation may not be loadable. This may be the case if objects
such as files, sockets or classes are included, as well as many other
objects which are not representable as Python literals.
The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don’t fit within the allowed width, adjustable by the width parameter defaulting to 80 characters.
Changed in version 3.9: Added support for pretty-printing types.SimpleNamespace.
Changed in version 3.10: Added support for pretty-printing dataclasses.dataclass.
Functions¶
- pprint.pp(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=False, underscore_numbers=False)¶
Prints the formatted representation of object, followed by a newline. This function may be used in the interactive interpreter instead of the
print()function for inspecting values. Tip: you can reassignprint = pprint.ppfor use within a scope.- Parameters:
object – The object to be printed.
stream (file-like object | None) – A file-like object to which the output will be written by calling its
write()method. IfNone(the default),sys.stdoutis used.indent (int) – The amount of indentation added for each nesting level.
width (int) – The desired maximum number of characters per line in the output. If a structure cannot be formatted within the width constraint, a best effort will be made.
depth (int | None) – The number of nesting levels which may be printed. If the data structure being printed is too deep, the next contained level is replaced by
.... IfNone(the default), there is no constraint on the depth of the objects being formatted.compact (bool) – Control the way long sequences are formatted. If
False(the default), each item of a sequence will be formatted on a separate line, otherwise as many items as will fit within the width will be formatted on each output line.sort_dicts (bool) – If
True, dictionaries will be formatted with their keys sorted, otherwise they will be displayed in insertion order (the default).underscore_numbers (bool) – If
True, integers will be formatted with the_character for a thousands separator, otherwise underscores are not displayed (the default).
>>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] >>> stuff.insert(0, stuff) >>> pprint.pp(stuff) [<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']
Added in version 3.8.
- pprint.pprint(