pprint.pformat()
pprint.pformat() is a Python function in the Pprint Module category. Same as pprint() but returns the formatted string instead of printing. The syntax is pprint.pformat(object, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False). Returns: Formatted string. A typical example: formatted = pprint.pformat(data, indent=2)
print(formatted). A close sibling is pprint.pprint(), which pretty-prints a Python object with proper indentation. A close sibling is pprint.PrettyPrinter(), which creates a PrettyPrinter instance for repeated use. More about this category: Pretty printer — pprint, pformat for readable output. Related Pprint Module entries: pprint.pprint(), pprint.PrettyPrinter(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details. You can try pprint.pformat() immediately in the Python playground, look up unfamiliar terms in the glossary, or return to the Python Reference homepage.