The pprint module in the standard library provide  a way for printing  data structures in an easy-to-read, formatted style. 

The pprint() is the basic function for pretty printing , this function will take an object and print the contents in a more organized than the regular print() function. The function makes it easy to clearly visualize the hierarchical structure of the data.The effect is especially visible if we are dealing with larger data structures, otherwise, the output  may appear just like with regular print()

ExampleEdit & Run

regular

data = {'Name':'John Doe',
       'Age':30,
       'Occupation': ['Software Engineer', 'Data Analyst'],
       'Hobbies':['Painting', 'Traveling', 'Cooking'], 
       'Education':{ 'Graduate':['Bachelor of Science','Masters of Science'], 
       'Undergraduate':['Bachelor of Arts','Masters of Arts'] } } 

print(data) 
copy
Output:
{'Name': 'John Doe', 'Age': 30, 'Occupation': ['Software Engineer', 'Data Analyst'], 'Hobbies': ['Painting', 'Traveling', 'Cooking'], 'Education': {'Graduate': ['Bachelor of Science', 'Masters of Science'], 'Undergraduate': ['Bachelor of Arts', 'Masters of Arts']}} [Finished in 0.011299045756459236s]

Compare the example below where the above data is pretty printed

ExampleEdit & Run
from pprint import pprint

data = {'Name':'John Doe',
       'Age':30,
       'Occupation': ['Software Engineer', 'Data Analyst'],
       'Hobbies':['Painting', 'Traveling', 'Cooking'], 
       'Education':{ 'Graduate':['Bachelor of Science','Masters of Science'], 
       'Undergraduate':['Bachelor of Arts','Masters of Arts'] } } 

pprint(data) 
copy
Output:
{'Age': 30,  'Education': {'Graduate': ['Bachelor of Science', 'Masters of Science'],                'Undergraduate': ['Bachelor of Arts', 'Masters of Arts']},  'Hobbies': ['Painting', 'Traveling', 'Cooking'],  'Name': 'John Doe',  'Occupation': ['Software Engineer', 'Data Analyst']} [Finished in 0.02891711052507162s]

Limiting nested Outputs

When the nesting in a data structure goes to deep, it may not be desirable to include all the details in the output. We can control the included nesting details by setting the depth parameter. The function will  only print as deep as the specified depth, adding a delimiter(...) to indicate that there is more data beyond the level. 

ExampleEdit & Run
from pprint import pprint

data = {'Name':'John Doe',
       'Age':30,
       'Occupation': ['Software Engineer', 'Data Analyst'],
       'Hobbies':['Painting', 'Traveling', 'Cooking'], 
       'Education':{ 'Graduate':['Bachelor of Science','Masters of Science'], 
       'Undergraduate':['Bachelor of Arts','Masters of Arts'] } } 

pprint(data, depth = 1) 
copy
Output:
{'Age': 30,  'Education': {...},  'Hobbies': [...],  'Name': 'John Doe',  'Occupation': [...]} [Finished in 0.028034217655658722s]

Customizing output width

The pprint() function by default uses a width of  80 characters, we can control this by setting the width parameter to the desired number of characters.

ExampleEdit & Run
from pprint import pprint

data = {'Name':'John Doe',
       'Age':30,
       'Occupation': ['Software Engineer', 'Data Analyst'],
       'Hobbies':['Painting', 'Traveling', 'Cooking'], 
       'Education':{ 'Graduate':['Bachelor of Science','Masters of Science'], 
       'Undergraduate':['Bachelor of Arts','Masters of Arts'] } } 

pprint(data, width = 100) 
copy
Output:
{'Age': 30,  'Education': {'Graduate': ['Bachelor of Science', 'Masters of Science'],                'Undergraduate': ['Bachelor of Arts', 'Masters of Arts']},  'Hobbies': ['Painting', 'Traveling', 'Cooking'],  'Name': 'John Doe',  'Occupation': ['Software Engineer', 'Data Analyst']} [Finished in 0.0274112056940794s]

Formatting 

The pformat() function is used to format the data without printing it. It returns a string with a formatted representation of the data structure. It is useful for generating organized textual representations of data structures especially for logging purposes.

ExampleEdit & Run
from pprint import pformat

data = {'Name':'John Doe',
       'Age':30,
       'Occupation': ['Software Engineer', 'Data Analyst'],
       'Hobbies':['Painting', 'Traveling', 'Cooking'], 
       'Education':{ 'Graduate':['Bachelor of Science','Masters of Science'], 
       'Undergraduate':['Bachelor of Arts','Masters of Arts'] } } 

formatted = pformat(data)
print(formatted)
copy
Output:
{'Age': 30,  'Education': {'Graduate': ['Bachelor of Science', 'Masters of Science'],                'Undergraduate': ['Bachelor of Arts', 'Masters of Arts']},  'Hobbies': ['Painting', 'Traveling', 'Cooking'],  'Name': 'John Doe',  'Occupation': ['Software Engineer', 'Data Analyst']} [Finished in 0.027740943245589733s]

pretty printing custom objects

In custom classes, pretty printing can be enabled by setting the __repr__() method  which will be used to generate and return a string containing a printable representation of an instance.

ExampleEdit & Run

  

from pprint import pprint 

class Car: 
    def __init__(self, make, model, year): 
         self.make = make 
         self.model = model 
         self.year = year 

    def __repr__(self): 
         return f'{self.make} {self.model} {self.year}' 

cars = [ Car('Ford', 'Mustang', 2004), Car('Ford', 'Fiesta', 2008), Car('Honda', 'Accord', 2012), Car('Toyota', 'Camry', 2018), ]

pprint(cars) 
copy
Output:
[Ford Mustang 2004, Ford Fiesta 2008, Honda Accord 2012, Toyota Camry 2018] [Finished in 0.02800869755446911s]