Skip to content

[Python] How to use "pprint()" function to print the pretty print

pprint

pprint() is an output method of standardized output in Python. A classic mode is to print the data type of Dictionary according to key-value from top to bottom.

It can make it easier for us to read the content of dictionary.

For example, I have a dictionary data type object:

animals = {'dog': 1, 'cat': 2, 'elephant': 3, 'alligator': 4, 'ape': 5, 'baboon': 6, 'bear': 7}



If I used built-in print() function to print:

print(animals)



Output:

{'dog': 1, 'cat': 2, 'elephant': 3, 'alligator': 4, 'ape': 5, 'baboon': 6, 'bear': 7}


Materials with less content are begging to be difficult to read. At this time, choose pprint() for better reading.

from pprint import pprint
pprint(animals)



Output:

{'alligator': 4,
 'ape': 5,
 'baboon': 6,
 'bear': 7,
 'cat': 2,
 'dog': 1,
 'elephant': 3}

References

Tags:

Leave a Reply