Python pprint Module
FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!
The pprint module, in Python, gives us the ability to “pretty-print” unformatted data in a well-formatted way.
so it comes ready to use in the Python Standard Library.
Importing the pprint Module in Python
We need to first import the pprint module to use the objects available in the pprint module. We can import it by using the import keyword.
Example of importing the pprint module
import pprint print(dir(pprint))
Output
In the above code example, we imported the pprint module and used the built-in dir() function to get a list of all available objects present in the pprint module.
Functions in pprint Module
1. pprint(obj, stream=sys.stdout)
This is the most used function in the pprint module. We can pass an arbitrary data structure to the function and it outputs the passed object in an easily readable way. By default, it outputs to the stdout but we can change this using the stream parameter.
Example on using pprint() in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]}
Output
‘cubes’: [1, 8, 27, 64, 125, 216, 343, 512, 729],
‘squares’: [1, 4, 9, 16, 25, 36, 49, 64, 81]}
2. print() vs pprint() in Python
The built-in print() and pprint() output to stdout. While the print() outputs the passed data as it is without any changes. The pprint() on the other hand modifies the passed data and outputs it in a more easily readable format.
Example of the difference between print() and pprint() in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} print("By using print()") print(data) print('By using pprint()') pprint.pprint(data)
Output
By using print(){‘squares’: [1, 4, 9, 16, 25, 36, 49, 64, 81], ‘cubes’: [1, 8, 27, 64, 125, 216, 343, 512, 729], ‘Biquadrates’: [1, 16, 81, 256, 625, 1296, 2401, 4096, 6561]}
By using pprint()
{‘Biquadrates’: [1, 16, 81, 256, 625, 1296, 2401, 4096, 6561],
‘cubes’: [1, 8, 27, 64, 125, 216, 343, 512, 729],
‘squares’: [1, 4, 9, 16, 25, 36, 49, 64, 81]}
In the above code example, we can see that the output we got using pprint() is more easily readable than the output we got using the built-in print().
3. pformat(obj)
The function returns a well-formatted string of the passed object. This is useful for logging and debugging our program.
Example on using the pformat() in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} fr_data = pprint.pformat(data) print(fr_data)
Output
‘cubes’: [1, 8, 27, 64, 125, 216, 343, 512, 729],
‘squares’: [1, 4, 9, 16, 25, 36, 49, 64, 81]}
pprint() vs pformat() in Python
Although outputs of both the functions look the same, there are a few differences between these functions. They are:
pprint() | pformat() |
Outputs the passed data to stdout. | Does not output anything. |
Has no return value. | Returns the formatted string of the passed data. |
The type of the output is the same as the type of the passed data. | The type of return value is a string. |
Parameters for pprint() and pformat() in Python
print() and pformat() have five common parameters that we can use to fine-tune their outputs.
Indent
This parameter controls the indentation of a line. We can pass the number of spaces we want to indent the lines. The default value of indent is 1.
Example on using indent in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} pprint.pprint(data, indent=10)
Output
‘cubes’: [1, 8, 27, 64, 125, 216, 343, 512, 729],
‘squares’: [1, 4, 9, 16, 25, 36, 49, 64, 81]}
Width
The width controls the length of a line. We can pass the maximum number of characters that can be printed in each line to this parameter. Exceeding characters will print in a new line. The default value of width is 80.
Example on using width in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} pprint.pprint(data, width=50)
Output
16,
81,
256,
625,
1296,
2401,
4096,
6561],
‘cubes’: [1, 8, 27, 64, 125, 216, 343, 512, 729],
‘squares’: [1, 4, 9, 16, 25, 36, 49, 64, 81]}
Depth
This parameter controls the number of depth levels it needs to output. The default value of this is None and it shows data at all depth levels by default. The representation “…” is used to display the data that is beyond the limit.
Example on using depth in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} pprint.pprint(data, depth=1)
Output
Compact
The default value of compact is False. Passing True will enable the function to merge complex data structures within lines.
Example on using compact in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 20)], "cubes": [pow(i, 3) for i in range(1, 20)], "Biquadrates":[pow(i, 4) for i in range(1, 20)]} pprint.pprint(data, compact=True)
Output
20736, 28561, 38416, 50625, 65536, 83521, 104976, 130321],
‘cubes’: [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744,
3375, 4096, 4913, 5832, 6859],
‘squares’: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256,
289, 324, 361]}
Sort_dicts
The default value of sort_dicts is True. It enables the function to sort key-value pairs in a dictionary by the key name. We can pass False to disable this sorting method.
Example on using sort_dicts in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} pprint.pprint(data, sort_dicts=False)
Output
{‘squares’: [1, 4, 9, 16, 25, 36, 49, 64, 81], ‘cubes’: [1, 8, 27, 64, 125, 216, 343, 512, 729],
‘Biquadrates’: [1, 16, 81, 256, 625, 1296, 2401, 4096, 6561]}
isreadable(Obj)
This function checks whether the formatted object of the passed object is readable or not. It returns True if it is readable, otherwise, it returns False. It always returns False for recursive objects. Readable objects can be later reconstructed by using the eval() function.
Example on using isreadable() in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} print(pprint.isreadable(data))
Output
isrecursive(obj)
This function checks whether the passed object needs a recursive representation or not. It returns True if it needs a recursive representation, otherwise, it returns False.
Example on using isrecursive() in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} print(pprint.isrecursive(data))
Output
saferepr(obj)
The function returns a string representation of an object. If the object has a recursive entry, then it will be displayed as <Recursion on typename with id=number>.
Example on using saferepr() in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} print(pprint.saferepr(data))
Output
Using the class PretyyPrinter in Python
Instead of using the default settings, we can create an object of PrettyPrinter to override and set new settings. We only need to specify the new settings once when we are initializing the object and we can use these settings throughout the program.
Example on using PrettyPrinter in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} ppr = pprint.PrettyPrinter(depth=1) ppr.pprint(data)
Output
pprint() vs PrettyPrinter().pprint() in Python
We need to specify our settings every time we use the pprint() of the default object. On the other hand, we need to specify our setting only once when we use the pprint() of the object we defined. This is useful when we need to use pprint() multiple times with custom settings rather than default settings.
Example on using pprint() in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} pprint.pprint(data, depth=1) pprint.pprint(data, depth=1) pprint.pprint(data, depth=1)
Output
{‘Biquadrates’: […], ‘cubes’: […], ‘squares’: […]}
{‘Biquadrates’: […], ‘cubes’: […], ‘squares’: […]}
In the above code example, we specified the depth value every time we used the pprint() function.
Example on using PrettyPrinter.pprint() in Python
import pprint data = {"squares":[pow(i, 2) for i in range(1, 10)], "cubes": [pow(i, 3) for i in range(1, 10)], "Biquadrates":[pow(i, 4) for i in range(1, 10)]} ppr = pprint.PrettyPrinter(depth=1) ppr.pprint(data) ppr.pprint(data) ppr.pprint(data)
Output
{‘Biquadrates’: […], ‘cubes’: […], ‘squares’: […]}
{‘Biquadrates’: […], ‘cubes’: […], ‘squares’: […]}
In the above code example, we specified the depth value only once while initializing the object.
Printing Recursive Data Structures Using pprint
We can also use the function pprint() to print recursive data structures. Since the data structures are recursive, the behavior of pprint() is a bit different. To understand this, let us first create a recursive data structure.
Example on creating a recursive data structure in Python
import pprint data = [1, 2, 3, 4] data.append(data) print(pprint.isrecursive(data))
Output
In the above code example, we created a recursive data structure.
Printing this data structure using pprint() prints only the first recursion. For remaining recursions, pprint() prints only their reference instead of recursing and printing the exact values.
Example on using pprint() for recursive data structures in Python
import pprint data = [1, 2, 3, 4] data.append(data) pprint.pprint(data)
Output
Python Interview Questions on the pprint Module
Q1. Write a program to check if the following data structure is recursive or not.
Data Structure – [1,2,3, [1,2,3]]
Ans 1. Complete code is as follows:
import pprint data_struct = [1,2,3,[1,2,3]] print(pprint.isrecursive(data_struct))
Output
Q2. Write a program to print a string representation of the following data structure using the pprint module.
Data Structure – {“apple”:[12,{“fruit”:”mango”}],”23″:23}
Ans 2. Complete code is as follows:
data_struct = {"apple": [12, {"fruit": "mango"}], "23": 23} print(pprint.saferepr(data_struct))
Output
Q3. Write a program to print the following dictionary without sorting.
Dictionary – {“B”: 2, 1:4, “A”:10}
Ans 3. Complete code is as follows:
import pprint dictionary = {"B": 2, 1:4, "A":10} pprint.pprint(dictionary, sort_dicts=False)
Output
Q4. Write a program to print the following data structure. Each line should contain at most ten characters.
Data structure – {“3”: [i*3 for i in range(1, 11)], “4”: [i*4 for i in range(1, 11)]}
Ans 4. Complete code is as follows
import pprint dat_struct = {"3": [i*3 for i in range(1, 11)], "4": [i*4 for i in range(1, 11)]} pprint.pprint(dat_struct, width=10)
Output
6,
9,
12,
15,
18,
21,
24,
27,
30],
‘4’: [4,
8,
12,
16,
20,
24,
28,
32,
36,
40]}
Q5. Write a program to print the following data. Every line should indent up to five characters.
Data- [{i: [i*j for j in range(1, 11)]} for i in range(1,6)]
Ans 5. Complete code is as follows:
import pprint data = [{i: [i*j for j in range(1, 11)]} for i in range(1,6)] pprint.pprint(data, indent=5)
Output
{2: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]},
{3: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]},
{4: [4, 8, 12, 16, 20, 24, 28, 32, 36, 40]},
{5: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]}]
Python pprint Module Quiz
Conclusion
In this article, we learned about the pprint module and the various functions available in the module. We also learned about the parameters of those functions, how to create custom objects, and their uses. Furthermore, if you have any queries, please feel free to share them with us in the comment section.