Andromeda
Note

Pretty Printing (Python)

Definition

The use of the pprint module to display complex data structures in a human-readable, formatted way.

Why It Matters

Computer data is meant for computers, not humans. Pretty printing is the “Cognitive Alignment” tool that lets us see the “Shape” of our data. Without it, you spend hours “parsing with your eyes,” which is the most error-prone activity a developer can do. It is the move from “State Blindness” to “State Clarity,” essential for debugging complex, nested systems.

Core Concepts

  • pprint.pprint(): Directly prints a formatted version of a dictionary or list, especially useful for nested data.
  • pprint.pformat(): Returns the formatted string instead of printing it.
  • Serialization Utility: The output of pformat() is valid Python source code. This is a basic way to save data to a .py file for future import.
  • Readability: Automatically adds indentation and line breaks to differentiate levels of nesting.
import pprint

message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
count = {}

for character in message:
    count.setdefault(character, 0)
    count[character] += 1

pprint.pprint(count)

Connected Concepts