The Wayback Machine - https://web.archive.org/web/20240108212022/https://www.geeksforgeeks.org/pprint-data-pretty-printer-python/
Open In App
Related Articles

pprint : Data pretty printer in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

This article is about a pretty useful built-in module in Python, pprint.

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a well-formatted and more readable way!

Let us consider an example:




# A python code without pprint
import requests
  
def geocode(address):
    resp = requests.get(url, params = {'address': address})
    return resp.json()
  
# calling geocode function
data = geocode('India gate')
  
# printing json response
print(data)


The above code is for getting the geocode information of a place using Google Maps API in JSON format.

The output of above program looks like this:

{'status': 'OK', 'results': [{'address_components': [{'long_name': 'Rajpath', 'types': ['route'], 
'short_name': 'Rajpath'}, {'long_name': 'India Gate', 'types': ['political', 'sublocality', 
'sublocality_level_1'], 'short_name': 'India Gate'}, {'long_name': 'New Delhi', 'types': 
['locality', 'political'], 'short_name': 'New Delhi'}, {'long_name': 'New Delhi', 
'types': ['administrative_area_level_2', 'political'], 'short_name': 'New Delhi'}, {'long_name': 
'Delhi', 'types': ['administrative_area_level_1', 'political'], 'short_name': 'DL'}, {'long_name': 
'India', 'types': ['country', 'political'], 'short_name': 'IN'}, {'long_name': '110001', 'types': 
['postal_code'], 'short_name': '110001'}], 'geometry': {'location': {'lng': 77.2295097, 'lat': 28.612912}, 
'viewport': {'northeast': {'lng': 77.2308586802915, 'lat': 28.6142609802915}, 'southwest': {'lng': 
77.22816071970848, 'lat': 28.6115630197085}}, 'location_type': 'APPROXIMATE'}, 'types': 
['establishment', 'point_of_interest'], 'formatted_address': 'Rajpath, India Gate, New Delhi, Delhi 110001, 
India', 'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc'}]}

As you can see, this output is not properly indented which affects readability for nested data structures.

Now, consider the code below:




# A python code with pprint
import requests
from pprint import pprint
  
def geocode(address):
    resp = requests.get(url, params = {'address': address})
    return resp.json()
  
# calling geocode function
data = geocode('India gate')
  
# pretty-printing json response
pprint(data)


The output of above code looks like this:

{'results': [{'address_components': [{'long_name': 'Rajpath',
                                      'short_name': 'Rajpath',
                                      'types': ['route']},
                                     {'long_name': 'India Gate',
                                      'short_name': 'India Gate',
                                      'types': ['political',
                                                'sublocality',
                                                'sublocality_level_1']},
                                     {'long_name': 'New Delhi',
                                      'short_name': 'New Delhi',
                                      'types': ['locality', 'political']},
                                     {'long_name': 'New Delhi',
                                      'short_name': 'New Delhi',
                                      'types': ['administrative_area_level_2',
                                                'political']},
                                     {'long_name': 'Delhi',
                                      'short_name': 'DL',
                                      'types': ['administrative_area_level_1',
                                                'political']},
                                     {'long_name': 'India',
                                      'short_name': 'IN',
                                      'types': ['country', 'political']},
                                     {'long_name': '110001',
                                      'short_name': '110001',
                                      'types': ['postal_code']}],
              'formatted_address': 'Rajpath, India Gate, New Delhi, Delhi '
                                   '110001, India',
              'geometry': {'location': {'lat': 28.612912, 'lng': 77.2295097},
                           'location_type': 'APPROXIMATE',
                           'viewport': {'northeast': {'lat': 28.6142609802915,
                                                      'lng': 77.2308586802915},
                                        'southwest': {'lat': 28.6115630197085,
                                                      'lng': 77.22816071970848}}},
              'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc',
              'types': ['establishment', 'point_of_interest']}],
 'status': 'OK'}

As you can see, the output is now well formatted and much more readable.

All we did was to import the pprint function of pprint module. And use pprint() function rather than the print function!

This blog is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 06 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials