PyDocs

Nav

Data Types

PrettyPrinter

pprint

class (source)
  • PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, expand=False, sort_dicts=True, underscore_numbers=False)
Construct a PrettyPrinter instance.
Changed in 3.4, 3.8, 3.10, 3.11, 3.15
  • 3.4: Added the compact parameter.
  • 3.8: Added the sort_dicts parameter.
  • 3.10: Added the underscore_numbers parameter.
  • 3.11: No longer attempts to write to sys.stdout if it is None.
  • 3.15: Added the expand parameter.
Members of PrettyPrinter

f

format
Format object for a specific context, returning a string and flags indicating whether the representation is 'readable' and whether the object represents a recursive construct.

i

isreadable
Determine if the formatted representation of the object is “readable,” or can be used to reconstruct the value using eval().
isrecursive
Determine if the object requires a recursive representation.

p

pformat
Return the formatted representation of object.
pprint
Print the formatted representation of object on the configured stream, followed by a newline.
1 Example
>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff[:])
>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(stuff)
[   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
    'spam',
    'eggs',
    'lumberjack',
    'knights',
    'ni']
>>> pp = pprint.PrettyPrinter(width=41, compact=True)
>>> pp.pprint(stuff)
[['spam', 'eggs', 'lumberjack',
  'knights', 'ni'],
 'spam', 'eggs', 'lumberjack', 'knights',
 'ni']
>>> pp = pprint.PrettyPrinter(width=41, expand=True, indent=3)
>>> pp.pprint(stuff)
[
   [
      'spam',
      'eggs',
      'lumberjack',
      'knights',
      'ni',
   ],
   'spam',
   'eggs',
   'lumberjack',
   'knights',
   'ni',
]
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
... ('parrot', ('fresh fruit',))))))))
>>> pp = pprint.PrettyPrinter(depth=6)
>>> pp.pprint(tup)
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
from the Python docs
See Also
No see-alsos for pprint/PrettyPrinter
0 Notes
No notes for PrettyPrinter