

{"id":36560,"date":"2019-03-01T12:18:45","date_gmt":"2019-03-01T06:48:45","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=36560"},"modified":"2026-04-24T14:55:03","modified_gmt":"2026-04-24T09:25:03","slug":"python-pprint","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-pprint\/","title":{"rendered":"Python pprint Module &#8211; Python Data Pretty Printer"},"content":{"rendered":"<p>Earlier, we discussed the Python OS Module. Today, we will see Python pprint, i.e. Python Pretty Print. Also, we will discuss Python pprint format and its example.<\/p>\n<p>This article includes recursive data structures and own classes in Pretty Print in Python. At last, we will see the Python pprint Module.<\/p>\n<p>So, let&#8217;s start the Python pprint tutorial.<\/p>\n<div id=\"attachment_36583\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-36583\" class=\"size-full wp-image-36583\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg\" alt=\"Python pprint Module - Python Data Pretty Printer\" width=\"1200\" height=\"630\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01-300x158.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01-768x403.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01-1024x538.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01-520x273.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-36583\" class=\"wp-caption-text\">Python pprint Module &#8211; Python Data Pretty Printer<\/p><\/div>\n<h3><span style=\"font-weight: 400\">What is Python Pretty Print?<\/span><\/h3>\n<p><span style=\"font-weight: 400\">The pprint module lets us <\/span><i><span style=\"font-weight: 400\">pretty-print <\/span><\/i><span style=\"font-weight: 400\">arbitrary data structures in Python to make them prettier, well-formatted, and more readable. What it gives us is in a form we can use as input to the interpreter. <\/span><\/p>\n<p><span style=\"font-weight: 400\">This is just the kind of thing for aesthetics, and it keeps the output on a single line wherever possible. And when on multiple lines, it indents it.<\/span><\/p>\n<p><strong>Key features of pretty print in Python:<\/strong><\/p>\n<ul>\n<li><strong>Automatic formatting:<\/strong> If the limit is exceeded, the object is broken into multiple lines and maintains visual alignment.<\/li>\n<li><strong>Sorted dictionaries:<\/strong> To make sure that there is consistency in the output, the dictionary keys are sorted in alphabetical order.<\/li>\n<li><strong>No infinite loops:<\/strong> If a list gets stuck in a loop, then the pprint will recognise the loop and print &lt; Recursion on\u2026.&gt; so you can identify where it repeats.<\/li>\n<li><strong>East customisation:<\/strong> You can change the way your data looks by setting the indent, width and depth.<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Let\u2019s import it:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; from pprint import pprint<\/pre>\n<p><span style=\"font-weight: 400\">Basically, this module has the following classes:<\/span><\/p>\n<p><b>class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False)<\/b><\/p>\n<p><span style=\"font-weight: 400\">This constructs a PrettyPrinter instance for us. Let\u2019s talk about its parameters:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><i><span style=\"font-weight: 400\">indent<\/span><span style=\"font-weight: 400\"> gives us the amount of indentation for each recursive level<\/span><\/i><\/li>\n<li style=\"font-weight: 400\"><i><span style=\"font-weight: 400\">width<\/span><span style=\"font-weight: 400\"> gives us the desired output width<\/span><\/i><\/li>\n<li style=\"font-weight: 400\"><i><span style=\"font-weight: 400\">depth<\/span><span style=\"font-weight: 400\"> gives us the number of levels to print<\/span><\/i><\/li>\n<li style=\"font-weight: 400\"><i><span style=\"font-weight: 400\">Stream<\/span><span style=\"font-weight: 400\"> lets us set an output stream<\/span><\/i><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Compact<\/span><span style=\"font-weight: 400\"> lets us fit as many items within the width as we can on each line of output<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Now, let\u2019s try an example without pprint. Let\u2019s take a list to work with:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; data=[(1,{'a':'A','b':'B','c':'C','d':'D'}),(2,{'e':'E','f':'F','g':'G','h':'H','i':'I','j':'J','k':'K','l':'L'}),(3,['m','n']),(4,['o','p','q','r','s','t','u','v','w']),(5,['x','y','z']),]\r\n&gt;&gt;&gt; print(data)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[(1, {&#8216;a&#8217;: &#8216;A&#8217;, &#8216;b&#8217;: &#8216;B&#8217;, &#8216;c&#8217;: &#8216;C&#8217;, &#8216;d&#8217;: &#8216;D&#8217;}), (2, {&#8216;e&#8217;: &#8216;E&#8217;, &#8216;f&#8217;: &#8216;F&#8217;, &#8216;g&#8217;: &#8216;G&#8217;, &#8216;h&#8217;: &#8216;H&#8217;, &#8216;i&#8217;: &#8216;I&#8217;, &#8216;j&#8217;: &#8216;J&#8217;, &#8216;k&#8217;: &#8216;K&#8217;, &#8216;l&#8217;: &#8216;L&#8217;}), (3, [&#8216;m&#8217;, &#8216;n&#8217;]), (4, [&#8216;o&#8217;, &#8216;p&#8217;, &#8216;q&#8217;, &#8216;r&#8217;, &#8216;s&#8217;, &#8216;t&#8217;, &#8216;u&#8217;, &#8216;v&#8217;, &#8216;w&#8217;]), (5, [&#8216;x&#8217;, &#8216;y&#8217;, &#8216;z&#8217;])]<\/div>\n<h3>Python pprint Example<\/h3>\n<p><span style=\"font-weight: 400\">So, let\u2019s try printing this with pprint instead.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(data)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[(1, {&#8216;a&#8217;: &#8216;A&#8217;, &#8216;b&#8217;: &#8216;B&#8217;, &#8216;c&#8217;: &#8216;C&#8217;, &#8216;d&#8217;: &#8216;D&#8217;}),(2,<\/p>\n<p>{&#8216;e&#8217;: &#8216;E&#8217;,<\/p>\n<p>&#8216;f&#8217;: &#8216;F&#8217;,<\/p>\n<p>&#8216;g&#8217;: &#8216;G&#8217;,<\/p>\n<p>&#8216;h&#8217;: &#8216;H&#8217;,<\/p>\n<p>&#8216;i&#8217;: &#8216;I&#8217;,<\/p>\n<p>&#8216;j&#8217;: &#8216;J&#8217;,<\/p>\n<p>&#8216;k&#8217;: &#8216;K&#8217;,<\/p>\n<p>&#8216;l&#8217;: &#8216;L&#8217;}),<\/p>\n<p>(3, [&#8216;m&#8217;, &#8216;n&#8217;]),<\/p>\n<p>(4, [&#8216;o&#8217;, &#8216;p&#8217;, &#8216;q&#8217;, &#8216;r&#8217;, &#8216;s&#8217;, &#8216;t&#8217;, &#8216;u&#8217;, &#8216;v&#8217;, &#8216;w&#8217;]),<\/p>\n<p>(5, [&#8216;x&#8217;, &#8216;y&#8217;, &#8216;z&#8217;])]<\/p>\n<\/div>\n<p><span style=\"font-weight: 400\">So, what does Python pprint do? It formats an object and writes it to the data stream we pass to it as an argument. By default, this is sys.stdout.<\/span><\/p>\n<h3>Python pprint Formatting<\/h3>\n<p><span style=\"font-weight: 400\">We can format a data structure without having to write it to a stream. To do this, we use pformat() to build a string representation, and possible use cases include logging.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import logging\r\n&gt;&gt;&gt; from pprint import pformat\r\n&gt;&gt;&gt; logging.basicConfig(\r\n       level=logging.DEBUG,\r\n       format='%(levelname)-8s %(message)s',)\r\n&gt;&gt;&gt; logging.debug('Logging pformatted data')<\/pre>\n<p><strong>DEBUG \u00a0\u00a0\u00a0Logging pformatted data<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; formatted=pformat(data)<\/pre>\n<p><span style=\"font-weight: 400\">So, we can print or log this string independently:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for line in formatted.splitlines():\r\n       logging.debug(line.rstrip())<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>DEBUG \u00a0\u00a0\u00a0[(1, {&#8216;a&#8217;: &#8216;A&#8217;, &#8216;b&#8217;: &#8216;B&#8217;, &#8216;c&#8217;: &#8216;C&#8217;, &#8216;d&#8217;: &#8216;D&#8217;}),DEBUG \u00a0\u00a0\u00a0\u00a0(2,<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0\u00a0{&#8216;e&#8217;: &#8216;E&#8217;,<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;f&#8217;: &#8216;F&#8217;,<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;g&#8217;: &#8216;G&#8217;,<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;h&#8217;: &#8216;H&#8217;,<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;i&#8217;: &#8216;I&#8217;,<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;j&#8217;: &#8216;J&#8217;,<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;k&#8217;: &#8216;K&#8217;,<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;l&#8217;: &#8216;L&#8217;}),<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0(3, [&#8216;m&#8217;, &#8216;n&#8217;]),<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0(4, [&#8216;o&#8217;, &#8216;p&#8217;, &#8216;q&#8217;, &#8216;r&#8217;, &#8216;s&#8217;, &#8216;t&#8217;, &#8216;u&#8217;, &#8216;v&#8217;, &#8216;w&#8217;]),<\/p>\n<p>DEBUG \u00a0\u00a0\u00a0\u00a0(5, [&#8216;x&#8217;, &#8216;y&#8217;, &#8216;z&#8217;])]<\/p>\n<\/div>\n<h3>Pretty-Printing Our Own Classes in Python<\/h3>\n<p><span style=\"font-weight: 400\">Last, we talked about the repr() built-in function in Python. In that, we observed the __repr__() method. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Today, let\u2019s see how we can make pprint work with our own classes by making use of the __repr__() method.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Color:\r\n       def __init__(self,name,hex_value):\r\n              self.name=name\r\n              self.hex_value=hex_value\r\n       def __repr__(self):\r\n              return(\r\n                      'I am '+self.name+' and you can find me at '+self.hex_value)\r\n&gt;&gt;&gt; colors=[Color('salmon','#FA8072'),Color('olive','#808000'),Color('purple','#800080')]\r\n&gt;&gt;&gt; print(colors)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[I am salmon and you can find me at #FA8072, I am olive and you can find me at #808000, I am purple and you can find me at #800080]<\/div>\n<p><span style=\"font-weight: 400\">Now see what Python pprint does to this:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(colors)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[I am salmon and you can find me at #FA8072,I am olive and you can find me at #808000,<\/p>\n<p>I am purple and you can find me at #800080]<\/p>\n<\/div>\n<h3>Python Pprint with Recursive Data Structures<\/h3>\n<p><span style=\"font-weight: 400\">Let\u2019s create a list and append it to its end. This makes for a recursive list.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; from pprint import pprint\r\n&gt;&gt;&gt; mylist=[1,2,'c','d']\r\n&gt;&gt;&gt; mylist.append(mylist)\r\n&gt;&gt;&gt; id(mylist)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">45553504<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(mylist)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, &#8216;c&#8217;, &#8216;d&#8217;, &lt;Recursion on list with id=45553504&gt;]<\/div>\n<p><span style=\"font-weight: 400\">#The recursive reference<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; mylist #Pay attention<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, &#8216;c&#8217;, &#8216;d&#8217;, [&#8230;]]<\/div>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Controlling Depth-<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">To control how far the Python pretty printer recurses down a nested structure, we can use the <\/span><i><span style=\"font-weight: 400\">depth <\/span><\/i><span style=\"font-weight: 400\">argument:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(data,depth=1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[(&#8230;), (&#8230;), (&#8230;), (&#8230;), (&#8230;)]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(data,depth=2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[(1, {&#8230;}), (2, {&#8230;}), (3, [&#8230;]), (4, [&#8230;]), (5, [&#8230;])]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(data,depth=3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[(1, {&#8216;a&#8217;: &#8216;A&#8217;, &#8216;b&#8217;: &#8216;B&#8217;, &#8216;c&#8217;: &#8216;C&#8217;, &#8216;d&#8217;: &#8216;D&#8217;}),<br \/>\n<span style=\"font-weight: 400\">\u00a0 \u00a0(2,<\/span>{&#8216;e&#8217;: &#8216;E&#8217;,&#8217;f&#8217;: &#8216;F&#8217;,&#8217;g&#8217;: &#8216;G&#8217;,&#8217;h&#8217;: &#8216;H&#8217;,<\/p>\n<p>&#8216;i&#8217;: &#8216;I&#8217;,<\/p>\n<p>&#8216;j&#8217;: &#8216;J&#8217;,<\/p>\n<p>&#8216;k&#8217;: &#8216;K&#8217;,<\/p>\n<p>&#8216;l&#8217;: &#8216;L&#8217;}),<\/p>\n<p>(3, [&#8216;m&#8217;, &#8216;n&#8217;]),<\/p>\n<p>(4, [&#8216;o&#8217;, &#8216;p&#8217;, &#8216;q&#8217;, &#8216;r&#8217;, &#8216;s&#8217;, &#8216;t&#8217;, &#8216;u&#8217;, &#8216;v&#8217;, &#8216;w&#8217;]),<\/p>\n<p>(5, [&#8216;x&#8217;, &#8216;y&#8217;, &#8216;z&#8217;])]<\/p>\n<\/div>\n<p><span style=\"font-weight: 400\">Here, the ellipses denote the levels it excludes from the output.<\/span><\/p>\n<h3>Deciding Output Width<\/h3>\n<p><span style=\"font-weight: 400\">We can decide the width for the output- this is the number of columns. The default is 80, but we can change that with <\/span><i><span style=\"font-weight: 400\">width<\/span><\/i><span style=\"font-weight: 400\">.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; mylist=[1,2,'c','d']\r\n&gt;&gt;&gt; pprint(mylist)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, &#8216;c&#8217;, &#8216;d&#8217;]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(mylist,width=-1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[1,2,<\/p>\n<p>&#8216;c&#8217;,<\/p>\n<p>&#8216;d&#8217;]<\/p>\n<\/div>\n<p><span style=\"font-weight: 400\">So, you can also pretty-print the output of the listdir() method or your system\u2019s environment variables with pprint(dict(os.environ),width=1).<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(os.listdir())<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;DLLs&#8217;,&#8217;Doc&#8217;,<\/p>\n<p>&#8216;etc&#8217;,<\/p>\n<p>&#8216;include&#8217;,<\/p>\n<p>&#8216;Lib&#8217;,<\/p>\n<p>&#8216;libs&#8217;,<\/p>\n<p>&#8216;LICENSE.txt&#8217;,<\/p>\n<p>&#8216;man&#8217;,<\/p>\n<p>&#8216;NEWS.txt&#8217;,<\/p>\n<p>&#8216;opencv_ffmpeg343.dll&#8217;,<\/p>\n<p>&#8216;out.log&#8217;,<\/p>\n<p>&#8216;python.exe&#8217;,<\/p>\n<p>&#8216;python3.dll&#8217;,<\/p>\n<p>&#8216;python37.dll&#8217;,<\/p>\n<p>&#8216;pythonw.exe&#8217;,<\/p>\n<p>&#8216;Scripts&#8217;,<\/p>\n<p>&#8216;share&#8217;,<\/p>\n<p>&#8216;tcl&#8217;,<\/p>\n<p>&#8216;Tools&#8217;,<\/p>\n<p>&#8216;vcruntime140.dll&#8217;]<\/p>\n<\/div>\n<p><span style=\"font-weight: 400\">You can use the <\/span><i><span style=\"font-weight: 400\">compact<\/span><\/i><span style=\"font-weight: 400\"> flag to make this more compact-<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprint(os.listdir(),compact=True)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;DLLs&#8217;, &#8216;Doc&#8217;, &#8216;etc&#8217;, &#8216;include&#8217;, &#8216;Lib&#8217;, &#8216;libs&#8217;, &#8216;LICENSE.txt&#8217;, &#8216;man&#8217;,&#8217;NEWS.txt&#8217;, &#8216;opencv_ffmpeg343.dll&#8217;, &#8216;out.log&#8217;, &#8216;python.exe&#8217;, &#8216;python3.dll&#8217;,<\/p>\n<p>&#8216;python37.dll&#8217;, &#8216;pythonw.exe&#8217;, &#8216;Scripts&#8217;, &#8216;share&#8217;, &#8216;tcl&#8217;, &#8216;Tools&#8217;,<\/p>\n<p>&#8216;vcruntime140.dll&#8217;]<\/p>\n<\/div>\n<h3>The pprintpp Module<\/h3>\n<p><span style=\"font-weight: 400\">Before we say goodbye, why don\u2019t we talk about pprintpp? This is another module for pretty printing and we like to call it pprint++. <\/span><\/p>\n<p><span style=\"font-weight: 400\">It is available on PyPI, and you can install it as:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Python pip install pprintpp<\/pre>\n<p><span style=\"font-weight: 400\">Let\u2019s import this.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import pprintpp<\/pre>\n<p><span style=\"font-weight: 400\">Now, let\u2019s try printing something with this.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pprintpp.pprint(data)<\/pre>\n<div id=\"attachment_36585\" style=\"width: 462px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/pprintpp.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-36585\" class=\"size-full wp-image-36585\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/pprintpp.png\" alt=\"Python pprint\" width=\"452\" height=\"323\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/pprintpp.png 452w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/pprintpp-150x107.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/pprintpp-300x214.png 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/a><p id=\"caption-attachment-36585\" class=\"wp-caption-text\">Python pprintpp module<\/p><\/div>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">pp-ez<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">You can also install the pp-ez package to do this:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; pp.pprint(data)<\/pre>\n<p><span style=\"font-weight: 400\">This gives us the same output. So how is pprint++ different from pprint? One difference is what you can see in the outputs. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Well, the goal of pprint++ is to emit a readable representation of the input that is also largely PEP-8 compliant.<\/span><\/p>\n<p>So, this was all in Python pprint. Hope you liked our explanation of Python Pretty Print.<\/p>\n<h3>Python Interview Questions on pprint Module<\/h3>\n<ol>\n<li>What is pprint in Python?<\/li>\n<li>How to use pprint in Python?<\/li>\n<li>How to make pretty print list in Python?<\/li>\n<li>What is the difference between print and pprint in Python?<\/li>\n<li>What does Python pretty print do?<\/li>\n<\/ol>\n<h3><span style=\"font-weight: 400\">Summary<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Hence, we discussed the Python pprint module and its method with its attributes. Some of these were width, depth, and compact. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Moreover, we looked at the example of Python&#8217;s pretty print. Also, we saw formatting in Pretty Print. Happy pretty-printing! This is the time to explore the <strong><a href=\"https:\/\/data-flair.training\/blogs\/top-python-interview-questions-answer\/\">most asked Python interview questions<\/a><\/strong>.\u00a0<\/span><\/p>\n<p>Still, if you have any queries regarding Python pprint, ask in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier, we discussed the Python OS Module. Today, we will see Python pprint, i.e. Python Pretty Print. Also, we will discuss Python pprint format and its example. This article includes recursive data structures and&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":36583,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[16763,16758,16757,16762,16761,16759,16760,16764],"class_list":["post-36560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-install-pprint-python","tag-pretty-print","tag-python-pprint","tag-python-pprint-multiline","tag-python-pprint-to-file","tag-python-pretty-print","tag-python-print-format","tag-what-is-pprint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python pprint Module - Python Data Pretty Printer - DataFlair<\/title>\n<meta name=\"description\" content=\"This Python PPrint Module tutorial consist of examples and ppring formatting.Here you will learn about Python Pretty Printer and recursive data structures\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-pprint\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python pprint Module - Python Data Pretty Printer - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This Python PPrint Module tutorial consist of examples and ppring formatting.Here you will learn about Python Pretty Printer and recursive data structures\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-pprint\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-01T06:48:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T09:25:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python pprint Module - Python Data Pretty Printer - DataFlair","description":"This Python PPrint Module tutorial consist of examples and ppring formatting.Here you will learn about Python Pretty Printer and recursive data structures","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-pprint\/","og_locale":"en_US","og_type":"article","og_title":"Python pprint Module - Python Data Pretty Printer - DataFlair","og_description":"This Python PPrint Module tutorial consist of examples and ppring formatting.Here you will learn about Python Pretty Printer and recursive data structures","og_url":"https:\/\/data-flair.training\/blogs\/python-pprint\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-03-01T06:48:45+00:00","article_modified_time":"2026-04-24T09:25:03+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python pprint Module &#8211; Python Data Pretty Printer","datePublished":"2019-03-01T06:48:45+00:00","dateModified":"2026-04-24T09:25:03+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/"},"wordCount":1157,"commentCount":5,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg","keywords":["install pprint python","pretty print","python pprint","python pprint multiline","Python pprint to file","python pretty print","python print format","what is pprint"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-pprint\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/","url":"https:\/\/data-flair.training\/blogs\/python-pprint\/","name":"Python pprint Module - Python Data Pretty Printer - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg","datePublished":"2019-03-01T06:48:45+00:00","dateModified":"2026-04-24T09:25:03+00:00","description":"This Python PPrint Module tutorial consist of examples and ppring formatting.Here you will learn about Python Pretty Printer and recursive data structures","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-pprint\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-pprint-01.jpg","width":1200,"height":630,"caption":"Python pprint Module - Python Data Pretty Printer"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-pprint\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python pprint Module &#8211; Python Data Pretty Printer"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/36560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=36560"}],"version-history":[{"count":10,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/36560\/revisions"}],"predecessor-version":[{"id":147836,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/36560\/revisions\/147836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/36583"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=36560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=36560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=36560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}