🐍 Python Beginner

What is Python's pprint module?

Answer

The pprint module (pretty-print) formats complex Python data structures in a human-readable way. from pprint import pprint; pprint(complex_dict, indent=2, width=80). For nested structures, pprint adds indentation and line breaks at appropriate places. pformat(data) returns the formatted string instead of printing. Key parameters: indent (indentation per level), width (line width limit), depth (max nesting depth before truncating with ...), compact (compact sequences on one line if they fit). Very useful when debugging large API responses, configuration objects, and deeply nested data. In Python 3.8+, prefer json.dumps(data, indent=2) for JSON-compatible data — it is often more readable. The rich third-party library provides even prettier output with syntax highlighting and tree views.