Skip to content

Instantly share code, notes, and snippets.

Last active March 28, 2023 16:26
What would you like to do?
Python pprint with color syntax highlighting for the console
from pprint import pformat
from typing import Any
from pygments import highlight
from pygments.formatters import Terminal256Formatter
from pygments.lexers import PythonLexer
def pprint_color(obj: Any) -> None:
"""Pretty-print in color."""
print(highlight(pformat(obj), PythonLexer(), Terminal256Formatter()), end="")
Copy link

I've updated the gist for Python 3 and added type hints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment