pprint (Pretty Printer) in Python
importox
Posted on June 9, 2020
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
Posted on June 9, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.