Learn by reading through in order

pprint — Display Nested dict and list Readably

Use pprint.pp to break the nested dict / list that print crams onto one line into indented form, and pformat to pass a formatted string into log.

When you use print while debugging, nested dictionaries and lists get crammed onto one unreadable line all the time. The pprint (pretty-print) module is a standard library tool that displays nested structures with indentation and newlines, which makes it invaluable for debugging and log output.

pp and pformat — One Prints, the Other Returns a String

pprint has two main characters. pprint.pp(value) is a function that formats and prints to stdout directly, returning None. pprint.pformat(value), on the other hand, returns the formatted string without printing anything to the screen.

pp vs pformat
nested valuedict / listpprint.pp(value)prints to stdoutreturns Nonenested valuedict / listpprint.pformat(value)returns theformatted string
pp formats and prints directly (returns None). pformat just returns the formatted string without printing anything. Use pformat when you want to massage the string before printing, or to embed it in a log line or error message.
FunctionPurposeReturns
pprint.pp(value)format and print to stdoutNone (prints directly)
pprint.pformat(value)return the formatted stringformatted string
indent=Nindent width (default 1)
width=Nmax line width (default 80)
sort_dicts=Falsepreserve dict key order (Python 3.8+)

Display Structure with pprint.pp

pprint.pp(value) and print(value) look the same for simple values, but their output diverges once you have nested dicts or listsprint jams everything onto one line, while pp adds line breaks and indentation matching the depth of the structure.

A nested order dictionary order is set up for you. Print it with both print and pprint.pp and compare the two visually.

① Print --- print --- first, then print(order).

② Print --- pprint.pp --- next, then pprint.pp(order).

(If you run this correctly, an explanation will appear.)

Python Editor

Run code to see output

Get the Formatted String with pprint.pformat

pprint.pformat(value) returns the formatted string without printing anything. Reach for pformat when you want to redirect the output (pass it to a logger, glue extra info before/after it and then print, or embed it in an error message). The width=N and indent=N options work the same as for pp.

A config dictionary config is set up for you. Format it with pformat and print it with a log-style label. Let pprint handle the formatting; you focus on treating the result as a string.

① Format config with pprint.pformat and width=40, storing the formatted string in a variable named formatted.

② Print the heading line [CONFIG] Settings at startup with print, then print(formatted).

Python Editor

Run code to see output
QUIZ

Knowledge Check

Answer each question one by one.

Q1Which function displays a nested dictionary while preserving its structure with newlines and indentation?

Q2Which one would you use to get a string from pprint and pass it to log output?

Q3How do you change pprint.pp's display width to 40 characters?