Python pprint (pretty print)

An article based on the Python pprint library.

Pretty printing is a feature that you will find in many programming languages, not just Python. pprint just happens to be the library in Python that provides this feature. But what is pretty printing?

One of the first things you’ll learn as a Python programming is the use of the print() to display data onto the screen. In fact, most people won’t every use anything other than this function to display data. And that’s OK, it works just fine as a general way of displaying data. However, there are certain situations where the print() will fail to display data in a user friendly manner. This is where the pprint() function comes in.


Python pprint

The first thing to do is demonstrate where to use pprint() over the standard print() function. We’ll use a simple example with a 2-Dimensional list to demonstrate the use of pprint.

The Python pprint library has a function called pprint() that we will be using.

import pprint

list1 = [["one","two","three","four","five"],
         ["six","seven","eight","nine","ten"],
         ["eleven","twelve","thirteen","fourteen","fifteen"]]

print(list1)
pprint.pprint(list1)

Compare the two outputs below and judge for yourself which one would prefer to see.

[['one', 'two', 'three', 'four', 'five'], ['six', 'seven', 'eight', 'nine', 'ten'], ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen']]

[['one', 'two', 'three', 'four', 'five'],
 ['six', 'seven', 'eight', 'nine', 'ten'],
 ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen']]

Pretty printing JSON

One instance where pretty print shines is when it comes to printing out JSON data. If you don’t know what JSON is, don’t worry. Just know that it’s a type of data format for data transfer across the internet (basically). We’re just using it because it shows the contrast between print() and pprint() very well.

Below we have some JSON data which we pulled from a Google maps search for New York. This is what it looks like when we used the print() function.

{'results': [{'access_points': [], 'address_components': [{'long_name': 'New York', 'short_name': 'New York', 'types': ['locality', 'political']}, {'long_name': 'New York', 'short_name': 'NY', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}], 'formatted_address': 'New York, NY, USA', 'geometry': {'bounds': {'northeast': {'lat': 40.9175771, 'lng': -73.70027209999999}, 'southwest': {'lat': 40.4773991, 'lng': -74.25908989999999}}, 'location': {'lat': 40.7127753, 'lng': -74.0059728}, 'location_type': 'APPROXIMATE', 'viewport': {'northeast': {'lat': 40.9175771, 'lng': -73.70027209999999}, 'southwest': {'lat': 40.4773991, 'lng': -74.25908989999999}}}, 'place_id': 'ChIJOwg_06VPwokRYv534QaPC8g', 'types': ['locality', 'political']}], 'status': 'OK'}

And this is what it looks like with the pprint() function. The data below is best viewed on a desktop screen.

{'results': [{'access_points': [],
              'address_components': [{'long_name': 'New York',
                                      'short_name': 'New York',
                                      'types': ['locality', 'political']},
                                     {'long_name': 'New York',
                                      'short_name': 'NY',
                                      'types': ['administrative_area_level_1',
                                                'political']},
                                     {'long_name': 'United States',
                                      'short_name': 'US',
                                      'types': ['country', 'political']}],
              'formatted_address': 'New York, NY, USA',
              'geometry': {'bounds': {'northeast': {'lat': 40.9175771,
                                                    'lng': -73.70027209999999},
                                      'southwest': {'lat': 40.4773991,
                                                    'lng': -74.25908989999999}},
                           'location': {'lat': 40.7127753, 'lng': -74.0059728},
                           'location_type': 'APPROXIMATE',
                           'viewport': {'northeast': {'lat': 40.9175771,
                                                      'lng': -73.70027209999999},
                                        'southwest': {'lat': 40.4773991,
                                                      'lng': -74.25908989999999}}},
              'place_id': 'ChIJOwg_06VPwokRYv534QaPC8g',
              'types': ['locality', 'political']}],
 'status': 'OK'}

Customizing pprint

Another bonus feature we get with this library is the ability to customize how our data is displayed. If you’re not satisfied with the default settings, you can change them by passing the appropriate input argument to the PrettyPrinter() function.

The PrettyPrinter() function creates a instance (object) of the pprint class. The benefit of this is that we get to pass additional parameters to change how the data is printed. We’ll re-use the first example for this section. The default settings are shown below.

class pprint.PrettyPrinter(indent=1width=80depth=Nonestream=None*compact=Falsesort_dicts=True)

indent is the number spaces used to indent each lines and width is the max limit of characters per line. We’ll now proceed to change the value for indent.

import pprint

pp = pprint.PrettyPrinter(indent = 4)

list1 = [["one","two","three","four","five"],
         ["six","seven","eight","nine","ten"],
         ["eleven","twelve","thirteen","fourteen","fifteen"]]

pp.pprint(list1)
[   ['one', 'two', 'three', 'four', 'five'],
    ['six', 'seven', 'eight', 'nine', 'ten'],
    ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen']]

Every line in the output above is now indented by 4 white-spaces.


This marks the end of the Python pprint Article. Any suggestions or contributions for CodersLegacy are more than welcome. Relevant questions can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments