How To Pretty Print in Python

How To Pretty Print in Python

Start using the pprint library

Image for postPhoto by Ilya Pavlov on Unsplash

Everybody needs to see the output of variables, calculations, and the progress through loops?one of the first functions you will learn to use is print().

It?s easy to stop here. The function gets the job done ? sort of. But if you?ve ever printed a dictionary, list, or any other complex data type, then you?ve experienced some of the pain points that come with print().

Formatting your print output is a huge quality of life improvement. Fortunately there is a simple solution in Python, the pprint library.

What is pprint?

Short for Pretty Printer, pprint is a native Python library that allows you to customize the formatting of your output. Refer to the official documentation for a full list of library and all properties available. In this tutorial we?ll go over basic usage of the pprint() method and PrettyPrinter() class.

Getting Started with pprint

To use pprint, begin by importing the library at the top of your Python file.

import pprint

From here you can either use the .pprint() method or instantiate your own pprint object with PrettyPrinter().

import pprintpprint.pprint(“Hello pretty printer”)my_printer = pprint.PrettyPrinter()my_printer.pprint(“Hello pretty printer”)

What is the Difference Between pprint() and PrettyPrinter() ?

The difference between the two is that the pprint() method will use the default settings of the library. We?ll get into what can be configured later, but for now, this means that each method call is independent of one another.

When you create your own PrettyPrinter() object, you can override those default configurations upon creation. Now, when using the pprint() method from your created object, the configurations you set will be used.

Settings are not stored with .pprint()

import pprintcoordinates = [ { “name”: “Location 1”, “gps”: (29.008966, 111.573724) }, { “name”: “Location 2”, “gps”: (40.1632626, 44.2935926) }, { “name”: “Location 3”, “gps”: (29.476705, 121.869339) }]pprint.pprint(coordinates, depth=1)# [{…}, {…}, {…}]pprint.pprint(coordinates)”””[{‘gps’: (29.008966, 111.573724), ‘name’: ‘Location 1’}, {‘gps’: (40.1632626, 44.2935926), ‘name’: ‘Location 2’}, {‘gps’: (29.476705, 121.869339), ‘name’: ‘Location 3’}]”””

Settings are stored with PrettyPrinter()

import pprintmy_printer = pprint.PrettyPrinter(depth=1)coordinates = [ { “name”: “Location 1”, “gps”: (29.008966, 111.573724) }, { “name”: “Location 2”, “gps”: (40.1632626, 44.2935926) }, { “name”: “Location 3”, “gps”: (29.476705, 121.869339) }]my_printer.pprint(coordinates)# [{…}, {…}, {…}]

So, if you want a one-off, then using the .pprint() method is the path of least resistance. In fact, if you plan to do this, import the method directly.

from pprint import pprintpprint([(3,5),(7,5),(4,3)])

What Can be Configured?

There are six parameters that can be configured with .pprint(). Here is each parameter, their default value in parenthesis, and a short description:

  • stream (None): When you want to pretty print to a file, use the stream property. Its default behavior?with a value of None?is to use sys.stdout
  • indent (1): The number of spaces to indent each line, this value can help when specific formatting is needed.
  • width (80): The maximum number of characters in each line. If a line exceeds this value, then the remaining text will be wrapped on the line(s) below.
  • depth (None): The number of depth levels (nested data types) to be displayed. The default is to show all data. When specified, data beyond the depth limit will be displayed as … nested in the appropriate data type syntax.
  • compact (False): When enabled, the compact property will consolidate complex data structures within lines as long as they do not exceed width limitations.
  • sort_dicts (True): By default, a pretty printed dictionary will display the key-value pairs sorted by the key name. When this is set to False, the dictionary keys will be displayed by the order of insertion.
24