DEV Community

importox
importox

Posted on

pprint (Pretty Printer) in Python

In this Python tutorial, you are going to learn about a very useful inbuilt module in python that is called pprint().

pprint is a python module that helps you to _make the readability of a complex data structures easy.

The pprint is also called as "prettyprint".

You can see an example below with a dictionary.

    # define dictionary
    dictionary = {"id": 1,"name": "Leanne Graham","username": "Bret","email": "Sincere@april.biz","address": {  "street": "Kulas Light",  "suite": "Apt. 556",  "city": "Gwenborough",  "zipcode": "92998-3874",  "geo": {    "lat": "-37.3159",    "lng": "81.1496"  }},"phone": "1-770-736-8031 x56442","website": "hildegard.org","company": {  "name": "Romaguera-Crona",  "catchPhrase": "Multi-layered client-server neural-net",  "bs": "harness real-time e-markets"}}

    # output the dictionary
    print(dictionary)

The above program outputs:

    {'id': 1, 'name': 'Leanne Graham', 'username': 'Bret', 'email': 'Sincere@april.biz', 'address': {'street': 'Kulas Light', 'suite': 'Apt. 556', 'city': 'Gwenborough', 'zipcode': '92998-3874', 'geo': {'lat': '-37.3159', 'lng': '81.1496'}}, 'phone': '1-770-736-8031 x56442', 'website': 'hildegard.org', 'company': {'name': 'Romaguera-Crona', 'catchPhrase': 'Multi-layered client-server neural-net', 'bs': 'harness real-time e-markets'}}

As you can see, the output is not in a proper and readable way , you cannot read this complex nested dictionary structure.

To solve this issue of readability you can use the inbuilt module pprint.

Downloading pprint Module

You can use pip to install the module. In your terminal or command prompt, type the following command:

pip install pprint

Using pycharm: Go to the project interpreter and install the module.

pprint example

Now after installing, import the module and there is a _function in this module named pprint so import that as _

from pprint import pprint

To make the structure look good just pprint() instead of print().

    from pprint import pprint

    dictionary = {"id": 1,"name": "Leanne Graham","username": "Bret","email": "Sincere@april.biz","address": {  "street": "Kulas Light",  "suite": "Apt. 556",  "city": "Gwenborough",  "zipcode": "92998-3874",  "geo": {    "lat": "-37.3159",    "lng": "81.1496"  }},"phone": "1-770-736-8031 x56442","website": "hildegard.org","company": {  "name": "Romaguera-Crona",  "catchPhrase": "Multi-layered client-server neural-net",  "bs": "harness real-time e-markets"}}

    # output the dictionary
    pprint(dictionary)

The above program outputs the clear and easily readable:

{'address': {'city': 'Gwenborough',
             'geo': {'lat': '-37.3159', 'lng': '81.1496'},
             'street': 'Kulas Light',
             'suite': 'Apt. 556',
             'zipcode': '92998-3874'},
 'company': {'bs': 'harness real-time e-markets',
             'catchPhrase': 'Multi-layered client-server neural-net',
             'name': 'Romaguera-Crona'},
 'email': 'Sincere@april.biz',
 'id': 1,
 'name': 'Leanne Graham',
 'phone': '1-770-736-8031 x56442',
 'username': 'Bret',
 'website': 'hildegard.org'}     

This also works in the Python shell

Top comments (1)

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

I love the simplicity of using pprint to make things like dictionary printouts so much easier to read. If its something that you are using very often you can add color fairly easy, but it does take quite a few more lines than pprint.

from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import JsonLexer
import json


dictionary = {"id": 1,"name": "Leanne Graham","username": "Bret","email": "Sincere@april.biz","address": {  "street": "Kulas Light",  "suite": "Apt. 556",  "city": "Gwenborough",  "zipcode": "92998-3874",  "geo": {    "lat": "-37.3159",    "lng": "81.1496"  }},"phone": "1-770-736-8031 x56442","website": "hildegard.org","company": {  "name": "Romaguera-Crona",  "catchPhrase": "Multi-layered client-server neural-net",  "bs": "harness real-time e-markets"}}


print(
    highlight(
        json.dumps(dictionary, sort_keys=True, indent=2,),
        JsonLexer(),
        TerminalFormatter(),
    )
)

Not nearly as simple, but more readable.