Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the pretty-print module in Python?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

The pprint module can be used to pretty-print the Python data structures in a readable format. The pprint is a native Python library.

We can use the pprint() method or create our pprint object with PrettyPrinter().

Syntax

pprint.pprint(object, stream = None, indent = 1, width = 80, depth = None,
compact = False, sort_dicts = True, underscore_numbers = False)

Parameters

The pprint() method takes the following parameters:

  1. object: It is the object to be pretty-printed.
  2. stream: It is a file-like object to which the output is written by calling its write() method. Its default value is None. This can be used when we want to write the pretty-print data to a file or a stream.
  3. indent: It denotes the amount of space added for each nesting level. The default value is 1.
  4. width: It denotes the maximum number of characters per line in the output. If the characters exceed the width, the remaining will be wrapped to the next line. The default value is 80.
  5. depth: It is the number of depth levels (nested data types) to be displayed. If the depth level of data is beyond the configured depth limit, then ... will be displayed. By default, it will show all data.
  6. compact: If compact is False (the default) then each item of a sequence will be formatted on a separate line. If compact is True, the maximum items that can fit within the width will be formatted on each output line.
  7. sort_dicts: If set to True, dictionaries will be formatted with their keys sorted. Otherwise, they will display in insertion order. The default value is True.
  8. underscore_numbers: Integers will be formatted with the _ character for a thousand separators. By default, it will not be displayed.

Code example

The below code demonstrates how to use the pprint() method:

import json
from urllib import request
import pprint
response = request.urlopen("https://jsonplaceholder.typicode.com/users/2")
json_response = response.read()
users = json.loads(json_response)
print("Without pretty print")
print(users)
print("With pretty print")
pprint.pprint(users)
print("With pretty print and depth 1 width 30")
pprint.pprint(users, depth = 1, width= 30)
Using pprint() to load JSON data

Code explanation

In the above code:

  • Lines 5 to 9: We load a dummy JSON object containing the user details from a URL. Then we print the loaded data using the normal print method.

Note: When using the print method, all the data is printed in an unformatted way.

  • Line 12 and 15: We use the pprint() method to pretty-print the JSON data with depth as 1 and width as 30.

Create custom pretty-print objects

We can configure the default configurations of the pprint method by creating the new PrettyPrinter() object. So we can use the object to pretty-print without specifying the configuration on each function call.

import json
from urllib import request
from pprint import PrettyPrinter
response = request.urlopen("https://jsonplaceholder.typicode.com/users/2")
json_response = response.read()
users = json.loads(json_response)
custom_printer = PrettyPrinter(
indent=4,
width=100,
depth=2,
compact=True,
sort_dicts=False
)
custom_printer.pprint(users)
Using a custom pprint object to load JSON data

Code explanation

In the above code:

  • Lines 9 to 15: We create a new PrettyPrinter object with custom configuration to pretty-print the data.
  • Line 17: We use the pprint method of the custom_printer object to pretty-print the user's data.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring