pprint module (Data pretty printer)


The pprint module (lib/pprint.py) is a part of Python’s standard library which is distributed along with standard Python distribution. The name pprint stands for pretty printer. The pprint module’s functionality enables aesthetically good looking appearance of Python data structures. Any data structure that can be correctly parsed by Python interpreter is elegantly formatted. The formatted expression is kept in one line as far as possible, but breaks into multiple lines if the length exceeds the width parameter of formatting. One unique feature of pprint output is that the dictionaries are automatically sorted before the display representation is formatted.

The pprint module contains definition of PrettyPrinter class. Its constructor takes following format −

pprint.PrettyPrinter(indent, width, depth, stream, compact)

The indent parameter defines indentation added on each recursive level. Default is 1.

The width parameter by default is 80. Desired output is restricted by this value. If the length is greater than width, it is broken in multiple lines.

The depth parameter controls number of levels to be printed.

The stream parameter is by default std.out – the default output device. It can take any stream object such as file.

The compact parameter id set to False by default. If true, only the data adjustable within width will be displayed.

The PrettyPrinter class defines following methods −

pprint() − prints the formatted representation of PrettyPrinter object

pformat() − Returns the formatted representation of object, based on parameters to the constructor.

Following example demonstrates simple use of PrettyPrinter class.

import pprint
students = {"Dilip":["English", "Maths", "Science"],
   "Raju":{"English":50,"Maths":60, "Science":70},
   "Kalpana":(50,60,70)}
pp = pprint.PrettyPrinter()
print ("normal print output")
print (students)
print ("----")
print ("pprint output")
pp.pprint(students)

The output shows normal as well as pretty print display.

normal print output
{'Dilip': ['English', 'Maths', 'Science'], 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}, 'Kalpana': (50, 60, 70)}
----
pprint output
{'Dilip': ['English', 'Maths', 'Science'],
'Kalpana': (50, 60, 70),
'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}

The pprint module also defines convenience functions pprint() and pformat() corresponding to PrettyPrinter methods. The example below uses pprint() function.

from pprint import pprint
students = {"Dilip":["English", "Maths", "Science"],
"Raju":{"English":50,"Maths":60, "Science":70},
"Kalpana":(50,60,70)}
print ("normal print output")
print (students)
print ("----")
print ("pprint output")
pprint (students)

Next example uses pformat() method as well as pformat() function. To use pformat() method, PrettyPrinter object is first set up. In both cases, the formatted representation is displayed using normal print() function.

import pprint
students = {"Dilip":["English", "Maths", "Science"],
"Raju":{"English":50,"Maths":60, "Science":70},
"Kalpana":(50,60,70)}
print ("using pformat method")
pp = pprint.PrettyPrinter()
string = pp.pformat(students)
print (string)
print ('------')
print ("using pformat function")
string = pprint.pformat(students)
print (string)

Here is the output of above code

using pformat method
{'Dilip': ['English', 'Maths', 'Science'],
'Kalpana': (50, 60, 70),
'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}
------
using pformat function
{'Dilip': ['English', 'Maths', 'Science'],
'Kalpana': (50, 60, 70),
'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}

Pretty printer can also be used with custom classes. Inside the class __repr__() method is overridden. The __repr__() method is called when repr() function is used. It is the official string representation of Python object. When we use object as parameter to print() function it prints return value of repr() function.

In following example, the __repr__() method returns the string representation of player object

import pprint
class player:
def __init__(self, name, formats = [], runs = []):
self.name = name
self.formats = formats
self.runs = runs
def __repr__(self):
dct = {}
dct[self.name] = dict(zip(self.formats,self.runs))
return (repr(dct))
l1 = ['Tests','ODI','T20']
l2 = [[140, 45, 39],[15,122,36,67, 100, 49],[78,44, 12, 0, 23, 75]]
p1 = player("virat",l1,l2)
pp = pprint.PrettyPrinter()
pp.pprint(p1)

The output of above code is −

{'virat': {'Tests': [140, 45, 39], 'ODI': [15, 122, 36, 67, 100, 49], 'T20': [78, 44, 12, 0, 23, 75]}}

Recursive data structure with pprint

When we try to print a recursive object with pprint, only first representation is displayed and for subsequent recursions, only its reference is printed.

>>> import pprint
>>> numbers = list(range(1,6))
>>> numbers.append(numbers)
>>> print (numbers)
[1, 2, 3, 4, 5, [...]]
>>> pprint.pprint(numbers)
[1, 2, 3, 4, 5, <Recursion on list with id=1403633698824>]

Restricting output width

If width parameter is changed from default 80 to other value, the output is formatted in such a way that multiple lines are displayed while care is taken not to violate the syntax.

import pprint
students = {"Dilip":["English", "Maths", "Science"],
"Raju":{"English":50,"Maths":60, "Science":70},
"Kalpana":(50,60,70)}
pp=pprint.PrettyPrinter(width = 20)
pp.pprint(students)

The code is similar to first example in this article. However, PrettyPrinter object is constructed with width parameter as 20. Hence the pprint output is accordingly formatted.

{'Dilip': [ 'English',
   'Maths',
   'Science'],
'Kalpana': (50,
   60,
   70),
'Raju': {'English': 50,
   'Maths': 60,
   'Science': 70}}

Updated on: 27-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements