5 min read

How to Use Python Pretty Print (pprint) for Cleaner Python Output

How to use python pretty print (pprint) for cleaner output

If you’re a Python developer, you likely require your debug output to be clear and well-structured. The python Pretty Print module (pprint), part of Python’s standard library, offers a convenient way to produce neatly formatted, readable output—particularly when working with complex data structures like dictionaries or JSON objects. Since pprint is built-in, no additional installation is needed, making it an efficient tool for enhancing the readability of logs and data during debugging.

What is pprint(), and what are its parameters?

It refers to “Pretty Print,” the native Python library that allows developers to customize their output experience with its different parameters and flags for a single class print (). This class effectively uses six parameters. Let’s explore a short overview of the different parameters with their default values.

  1. indent:  The number of spaces to indent each line. This value is used if you require specific formatting. Its default value is 1.
  2. width: It refers to the maximum number of characters in a single line. The following text will be wrapped on the lines below if the word limit exceeds. Its default value is 80.
  3. depth: The number of depth levels displayed if utilizing nested data types. It shows all the data by default, but if you want to specify, the beyond data will be shown in the form of dots (…). Its default value is None.
  4. stream:  Stream is used if developers want to specify output and is primarily used to print a file. By default, its behavior is to use sys.stdout. Its default value is also None. 
  5. compact: This is a Boolean argument. If set to True, it will help to adjust a complex data structure into a single line within a specified width. By default, it is set to False, meaning all the items will be formatted to the next separate line.
  6. sort_dicts: This is also a Boolean argument used to print dictionaries with pprint(). It prints the sorted key value pair based on alphabetically. The key value prints based on their order insertion if it is set to False. Its default value is True. 

How is Python Pretty Print (pprint) Different from print?

Let’s compare the two:

  • print() is great for simple output—strings, numbers, or shortlists.
  • pprint() is best for structured, nested, or complex data.

For example, if you want to work with JSON data from an API, or parsing log files, or debug Python objects with lots of levels, pprint should be your first choice that will save time. 

Basics of pprint()

Firstly, you need to import the pprint module.

import pprint

You can use the pprint() method or instantiate your pprint object with PrettyPrinter().

import pprint

pprint.pprint("Hello World!")
my_printer = pprint.PrettyPrinter()
my_printer.pprint("Hello Pretty Printer")
print(type(my_printer))

Now you can create a sample dictionary to show some of the arguments of the class pprint().

sample_dict = {

    'name': 'Sion',

    'age': 20,

    'message': 'Thank you for reading this article',

    'topic':'Python Libraries'

}

If you print the output without using pprint(), you can get the following output: 

{'name': 'Sion', 'age': 20, 'message': 'Thank you for reading this article!', 'topic': 'Python Libraries'}

The output is very appealing, but the output format is still okay, as you can clearly see which value belongs to which key.  The problem occurs if these values are incredibly long and nested or if there are more key-value pairs. That’s where you need to use a pretty print Python library to get the most visually appealing output. For example:

pprint.pprint(sample_dict)

{'age': 21,
 'message': 'Thank you for reading this article!',
 'name': 'Sion',
 'topic': 'Python Libraries'}

You can check that all the key pairs are in a separate row, which allows reading easily. Also, if you check closely, all the elements are automatically sorted according to the keys.

The pprintpp Module

The pprintpp module is one of the most improved versions of the built-in pprint module in Python. By leveraging it, developers can get modern pretty-printing features, allowing them to format complex data structures in easy-to-read, more attractive-looking output. With pprintpp, they can easily manage nested objects, dictionaries, lists, and other data structures. 

Fortunately, it also offers enhanced formatting options, allowing developers to analyze large amounts of data in an organized and clear form. Now it’s time for developers to choose pprintpp in Python library for their data visualization and debugging capabilities. 

Can I Use python Pretty Print (pprint) with JSON?

Yes, you can easily use Python’s pprint module with JSON data. It is the most preferred choice when dealing with JSON from an API, a file, or a web service. JSON (JavaScript Object Notation) is a standard data format utilized to easily exchange data between systems—more importantly, in web and mobile applications.

JSON data is usually structured like a dictionary in Python. It often includes nested elements like objects inside objects or lists inside dictionaries. When you try to print raw JSON using the standard print() function, the output can be confusing and hard to read. That’s where pprint becomes helpful.

import json
import pprint

json_string = '{"name": "Olivia", "age": 27, "hobbies": ["cycling", "baking", "music"]}'
data = json.loads(json_string)

pp = pprint.PrettyPrinter()
pp.pprint(data)

Final Thoughts

As a Python developer, you’ll find that pprint is a simple yet powerful tool to improve how you work with complex data. Whether you’re debugging nested structures or just want your output to be easier on the eyes, pprint helps make your data more readable and manageable. If you’re looking to level up your Python or data science workflow, getting comfortable with pprint() is a small investment that pays off quickly.

Frequently Asked Questions

  1. Is pprint a built-in module in Python?
    No. pprint is not built in like functions such as print() or len(). It is part of the Python Standard Library, which means it comes with Python and is ready to use after installation. You just need to import it.
  2. Is Pprint native to Python?
    Yes. pprint is included in the Python Standard Library and is available in every standard Python installation. It does not require any additional packages or setup.

Previous Post
Production-Ready FastAPI Deployment Using Docker and Uvicorn
Next Post
Top Managed PostgreSQL Services Compared (2025 Edition)

Deploy Django, Flask, and FastAPI in seconds

Seenode simplifies the deployment of your apps with a single, scalable, easy-to-use platform.

Share

How to Use Python Pretty Print (pprint) for Cleaner Python Output

How to use python pretty print (pprint) for cleaner output

If you’re a Python developer, you likely require your debug output to be clear and well-structured. The python Pretty Print module (pprint), part of Python’s standard library, offers a convenient way to produce neatly formatted, readable output—particularly when working with complex data structures like dictionaries or JSON objects. Since pprint is built-in, no additional installation is needed, making it an efficient tool for enhancing the readability of logs and data during debugging.

What is pprint(), and what are its parameters?

It refers to “Pretty Print,” the native Python library that allows developers to customize their output experience with its different parameters and flags for a single class print (). This class effectively uses six parameters. Let’s explore a short overview of the different parameters with their default values.

  1. indent:  The number of spaces to indent each line. This value is used if you require specific formatting. Its default value is 1.
  2. width: It refers to the maximum number of characters in a single line. The following text will be wrapped on the lines below if the word limit exceeds. Its default value is 80.
  3. depth: The number of depth levels displayed if utilizing nested data types. It shows all the data by default, but if you want to specify, the beyond data will be shown in the form of dots (…). Its default value is None.
  4. stream:  Stream is used if developers want to specify output and is primarily used to print a file. By default, its behavior is to use sys.stdout. Its default value is also None. 
  5. compact: This is a Boolean argument. If set to True, it will help to adjust a complex data structure into a single line within a specified width. By default, it is set to False, meaning all the items will be formatted to the next separate line.
  6. sort_dicts: This is also a Boolean argument used to print dictionaries with pprint(). It prints the sorted key value pair based on alphabetically. The key value prints based on their order insertion if it is set to False. Its default value is True. 

How is Python Pretty Print (pprint) Different from print?

Let’s compare the two:

  • print() is great for simple output—strings, numbers, or shortlists.
  • pprint() is best for structured, nested, or complex data.

For example, if you want to work with JSON data from an API, or parsing log files, or debug Python objects with lots of levels, pprint should be your first choice that will save time. 

Basics of pprint()

Firstly, you need to import the pprint module.

import pprint

You can use the pprint() method or instantiate your pprint object with PrettyPrinter().

import pprint

pprint.pprint("Hello World!")
my_printer = pprint.PrettyPrinter()
my_printer.pprint("Hello Pretty Printer")
print(type(my_printer))

Now you can create a sample dictionary to show some of the arguments of the class pprint().

sample_dict = {

    'name': 'Sion',

    'age': 20,

    'message': 'Thank you for reading this article',

    'topic':'Python Libraries'

}

If you print the output without using pprint(), you can get the following output: 

{'name': 'Sion', 'age': 20, 'message': 'Thank you for reading this article!', 'topic': 'Python Libraries'}

The output is very appealing, but the output format is still okay, as you can clearly see which value belongs to which key.  The problem occurs if these values are incredibly long and nested or if there are more key-value pairs. That’s where you need to use a pretty print Python library to get the most visually appealing output. For example:

pprint.pprint(sample_dict)

{'age': 21,
 'message': 'Thank you for reading this article!',
 'name': 'Sion',
 'topic': 'Python Libraries'}

You can check that all the key pairs are in a separate row, which allows reading easily. Also, if you check closely, all the elements are automatically sorted according to the keys.

The pprintpp Module

The pprintpp module is one of the most improved versions of the built-in pprint module in Python. By leveraging it, developers can get modern pretty-printing features, allowing them to format complex data structures in easy-to-read, more attractive-looking output. With pprintpp, they can easily manage nested objects, dictionaries, lists, and other data structures. 

Fortunately, it also offers enhanced formatting options, allowing developers to analyze large amounts of data in an organized and clear form. Now it’s time for developers to choose pprintpp in Python library for their data visualization and debugging capabilities. 

Can I Use python Pretty Print (pprint) with JSON?

Yes, you can easily use Python’s pprint module with JSON data. It is the most preferred choice when dealing with JSON from an API, a file, or a web service. JSON (JavaScript Object Notation) is a standard data format utilized to easily exchange data between systems—more importantly, in web and mobile applications.

JSON data is usually structured like a dictionary in Python. It often includes nested elements like objects inside objects or lists inside dictionaries. When you try to print raw JSON using the standard print() function, the output can be confusing and hard to read. That’s where pprint becomes helpful.

import json
import pprint

json_string = '{"name": "Olivia", "age": 27, "hobbies": ["cycling", "baking", "music"]}'
data = json.loads(json_string)

pp = pprint.PrettyPrinter()
pp.pprint(data)

Final Thoughts

As a Python developer, you’ll find that pprint is a simple yet powerful tool to improve how you work with complex data. Whether you’re debugging nested structures or just want your output to be easier on the eyes, pprint helps make your data more readable and manageable. If you’re looking to level up your Python or data science workflow, getting comfortable with pprint() is a small investment that pays off quickly.

Frequently Asked Questions

  1. Is pprint a built-in module in Python?
    No. pprint is not built in like functions such as print() or len(). It is part of the Python Standard Library, which means it comes with Python and is ready to use after installation. You just need to import it.
  2. Is Pprint native to Python?
    Yes. pprint is included in the Python Standard Library and is available in every standard Python installation. It does not require any additional packages or setup.

Previous Post
Production-Ready FastAPI Deployment Using Docker and Uvicorn
Next Post
Top Managed PostgreSQL Services Compared (2025 Edition)

Share