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()
.
Compare the example below where the above data is pretty printed
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.
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.
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.
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.