How do I get Python's pprint to return a string instead of printing?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python's pprint module is for readable formatting of nested data structures, but pprint.pprint writes directly to a stream instead of returning a value. If you need the formatted result as a string for logs, tests, files, or APIs, the correct function is pprint.pformat.
pprint Versus pformat
The two names are similar, but they do different jobs:
- '
pprint.pprint(obj)formats and writes the output' - '
pprint.pformat(obj)formats and returns a string'
The behavior is easy to see in a short example.
The call to pprint.pprint sends text to standard output. The call to pprint.pformat gives you a regular Python string.
Why pformat Is Usually the Right Answer
Returning a string is useful when formatting is part of a larger workflow instead of the final output step. Common cases include:
- adding pretty output to a log message
- storing formatted diagnostics in a file
- asserting against readable snapshots in tests
- building debug sections in an HTTP response or CLI report
Using pformat keeps that decision in your code instead of forcing everything through printing.
Controlling the Formatting
pformat accepts many of the same options as pprint.
These options are worth knowing:
- '
indentchanges nesting indentation' - '
widthcontrols line wrapping' - '
compactreduces vertical space for short collections' - '
sort_dictsaffects dictionary key order'
That makes pformat useful not just for readability, but for stable output in automated tests.
Reusing a PrettyPrinter
If you pretty-print many objects with the same style, create a PrettyPrinter once and reuse it.
This is cleaner than passing the same keyword arguments repeatedly throughout a codebase.
Capturing pprint Output Is Usually the Wrong Tool
You can technically redirect standard output or pass a different stream to pprint.pprint, but that is usually more awkward than simply calling pformat.
This works, but it is mainly useful when an API insists on stream-based writing. If your goal is "give me a string," pformat is the simpler and more direct option.
A Useful Logging Pattern
Pretty-printed strings are especially helpful in debugging logs.
This keeps the log call explicit and avoids mixing printing with structured logging code.
Common Pitfalls
The biggest pitfall is expecting pprint.pprint to return a string. It returns None because its job is side-effect output, not value production.
Another issue is comparing pretty-printed text in tests without controlling options like width or sort_dicts. If formatting settings vary, your snapshots become noisy and fragile.
Developers also misuse pretty-printed output as a serialization format. pformat is for human readability, not for machine-to-machine data exchange. Use JSON, YAML, or another real serialization format when the output needs to be parsed later.
Finally, do not forget that very large or recursive structures can still produce output that is expensive to generate or hard to read, even when pretty-printed.
Summary
- Use
pprint.pformatwhen you need formatted data as a string. - Use
pprint.pprintonly when direct stream output is the goal. - '
pformatsupports indentation, width, compactness, and dictionary ordering options.' - '
PrettyPrinter().pformat(...)is useful when you want reusable formatting rules.' - Pretty-printed text is for humans, not as a substitute for real serialization formats.

