pprint (Pretty Printer) in Python
Tags: python
Published: 2020-06-09 10:49:10

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

<a href="https://docs.python.org/3/library/pprint.html">pprint</a> 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 <a href="https://pythonprogramminglanguage.com/dictionary/">dictionary</a>.    

```python      
    # 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:    
 
```python   
    {'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 <a href="https://python-commandments.org/create-a-dictionary-in-python/">dictionary</a> structure.

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

## Downloading pprint Module

You can use <a href="https://pythonbasics.org/how-to-use-pip-and-pypi/">pip to install</a> 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 <a href="https://pythonspot.com/python-strings/">print()</a>.
       
```python
    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:
  
    
```python
{'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 <a href="https://bsdnerds.org/learn-python/">Python shell</a>