Python @ DjangoSpin

Python: Prettifying output with the builtin pprint module

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Prettifying output in Python using pprint

Prettifying output in Python using pprint

The builtin pprint module enables the user to prettify the output while printing lists, tuples & dictionaries. This is extremely useful while having a closer look at these data structures.

</p>
>>> years = {
	'1960s': [1961, 1962, 1963], '1970s': [1971, 1972, 1973],
	 '1980s': [1981, 1982, 1983]
	}
>>> years
{'1970s': [1971, 1972, 1973], '1980s': [1981, 1982, 1983], '1960s': [1961, 1962, 1963]}


>>> import pprint
>>> pprint.pprint(years)
{'1960s': [1961, 1962, 1963],
 '1970s': [1971, 1972, 1973],
 '1980s': [1981, 1982, 1983]}
 
 
>>> pprint.pprint(years, indent = 8)
{       '1960s': [1961, 1962, 1963],
        '1970s': [1971, 1972, 1973],
        '1980s': [1981, 1982, 1983]}
		
>>> pprint.pprint(years, width = 20)
{'1960s': [1961,
           1962,
           1963],
 '1970s': [1971,
           1972,
           1973],
 '1980s': [1981,
           1982,
           1983]}
		   
		   
>>> pprint.pprint(years, depth = 1)
{'1960s': [...], '1970s': [...], '1980s': [...]}

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply