{"id":80172,"date":"2021-01-12T09:00:20","date_gmt":"2021-01-12T03:30:20","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80172"},"modified":"2021-01-12T09:00:20","modified_gmt":"2021-01-12T03:30:20","slug":"python-pprint-module","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/","title":{"rendered":"Pretty Printer in Python (pprint Module)"},"content":{"rendered":"<p>The fairy basket has a lot more to offer, Right?<\/p>\n<p>Taking no time, let&#8217;s start with yet one more important function of Python, Pprint().<\/p>\n<p>The pprint module gives the capability to \u201cpretty-print\u201d arbitrary Python data structures in such a form that can be used as input to the interpreter.<\/p>\n<p>And if the formatted structures include objects which are not fundamental Python types, then the representation is not loadable.<\/p>\n<h2>What is Pretty Print in Python?<\/h2>\n<p>Pretty print acts as a source code which helps in the formatting conventions and markup of small codes in a python program.<\/p>\n<p>What makes pretty print different from any other function is that the coder can simultaneously check and correct the mistake in a code.<\/p>\n<p><strong>Python pprint Module Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from pprint import pprint\nx= \"Welcome to TechVidvan\"\nprint(x)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Welcome to TechVidvan<\/div>\n<h2>Pprint Module in Python<\/h2>\n<p>The simplest way to use Pprint is as given in the above code.<\/p>\n<p>The simplicity of using the Pprint module is to make things like dictionary printouts so much easier to read. And a coder can do this in a fairly easy manner.<\/p>\n<p>In the pprint module, the function prints to standard out while the format function returns a string that is likely to be useful in logging also for other output requirements.<\/p>\n<p>Here is something the fairy basket has to offer as a bonus, to the ones who are new to Pprint.<\/p>\n<p>The above information is quite enough to get started and do your own code, but wait.<\/p>\n<p>Do you have any ideas about python files? Then tune with me, Pprint() does something miraculous here too.<\/p>\n<p><strong>Python pprint example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">list=[1,2,3,4,5]\n&gt;&gt;&gt;print(list)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">y<br \/>\n[1, 2, 3, 4, 5]<\/div>\n<h3>JSON Pretty print in Python<\/h3>\n<p>JSON Pretty print is a function in datafile library which simply executes the print method in any of its saved file.<\/p>\n<p>There are two examples listed below:<\/p>\n<h4>1. Example of JSON Pretty Print using Python<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import json\nhijson = '{ \"website\": \"TechVidvan\", \"topic\": \"Pprint\"}\u2019\nparsed =json.loads(hijson)\nprint json.dumps (parsed, indent=2, sort_keys= True)\n<\/pre>\n<h4>2. Example of JSON print from File using Python<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import Json\nwith open ('json TechVidvan.txt') as jasonfile:\n         parsed= json.load(json TechVidvan)\nprint json.dumps(parsed,indent=2, sort_keys= True)\n<\/pre>\n<p>The program will write, read, and create Python objects and dump the Json Data in a string.<\/p>\n<h3>Recursive Data Structure with Pprint<\/h3>\n<p>Pretty Print proves to be very beneficial when repetitive data is entered by the user.<\/p>\n<p>It treats all the values as different yet in the same variable, which not only makes the code efficient but also saves the time of the coder.<\/p>\n<p><strong>Example of Recursive Data Structure with Pprint<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; from pprint import pprint\nmylist=['a','b','c','d']\nmylist.append(\"t\")\nprint(mylist)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">y<br \/>\n[&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;, &#8216;t&#8217;]<\/div>\n<h3>Controlling Output Width in Python<\/h3>\n<p>The actual output width for the formatted text is 80 columns. To adjust that width, use the width argument to pprint().<\/p>\n<p><strong>Python pprint example to control Output Width<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist=['a','b','c','d']\nmylist.append(\"t\")\n\nfor i in mylist:\n    del (i)\nprint(mylist)\n<\/pre>\n<p><strong>Output:\u00a0<\/strong><\/p>\n<div class=\"code-output\">y<br \/>\n[&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;, &#8216;t&#8217;]<\/div>\n<h3>Limiting Nested Output in Python<\/h3>\n<p>For the deep data structures, you may not need the output to include all of the details. It might be impossible to format the code properly, the formatted text might be too large to manage, or you may need all of it again.<\/p>\n<p>In that case, the depth argument can control how far down into the nested data structure the pretty-printer goes and executes.<\/p>\n<p><strong>Example of python limiting nested Output<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">for width_1 in [ 80, 20, 5 ]:\n    print (\"WIDTH  =\", width_1)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">y<br \/>\nWIDTH = 80<br \/>\nWIDTH = 20<br \/>\nWIDTH = 5<\/div>\n<h3>Python Pprint Formatting<\/h3>\n<p>We can format the data structure without having to write it in a stream. For doing this, we use pformat() to build a string representation, which includes logging.<\/p>\n<p><strong>Python pprint Formatting Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; import logging\n&gt;&gt;&gt; from pprint import pformat\n&gt;&gt;&gt; logging.basicConfig\n       level=logging.DEBUG,\n       format='%(levelname)-7s %(message)s',)\n&gt;&gt;&gt; logging.debug('Logging pformatted data')\n<\/pre>\n<h4>Arbitrary Classes in Python<\/h4>\n<p>The arbitrary classes in python act as a normal printing dictionary of the code, which copies the data from the library and executes it in the code. And finally prints it in the source code as a copy.<\/p>\n<h2>Summary<\/h2>\n<p>Hence, we discussed one more magic spell from the basket. The Python pprint module and its method with its attributes and execution.<\/p>\n<p>The major among these are width, depth, and compact. Moreover, we looked at the example codes of Python pretty print.<\/p>\n<p>Then for the advanced coders who are well versed with python binary data files, we learned how Json uses pprint to read, append as well as write python static\/dynamic files.<\/p>\n<p>Towards the end of the article, we learned how we can actually use this simple yet very useful module in various programs of Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The fairy basket has a lot more to offer, Right? Taking no time, let&#8217;s start with yet one more important function of Python, Pprint(). The pprint module gives the capability to \u201cpretty-print\u201d arbitrary Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80175,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3343,3344,3345],"class_list":["post-80172","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-pretty-printer-in-python","tag-pretty-printer-module-in-python","tag-python-pprint-module"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Pretty Printer in Python (pprint Module) - TechVidvan<\/title>\n<meta name=\"description\" content=\"Pretty printer provides a capability to \u201cpretty-print\u201d arbitrary Python data structures in a form which can be used as input to interpreter.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pretty Printer in Python (pprint Module) - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Pretty printer provides a capability to \u201cpretty-print\u201d arbitrary Python data structures in a form which can be used as input to interpreter.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-12T03:30:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Pprint-Module.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pretty Printer in Python (pprint Module) - TechVidvan","description":"Pretty printer provides a capability to \u201cpretty-print\u201d arbitrary Python data structures in a form which can be used as input to interpreter.","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:\/\/techvidvan.com\/tutorials\/python-pprint-module\/","og_locale":"en_US","og_type":"article","og_title":"Pretty Printer in Python (pprint Module) - TechVidvan","og_description":"Pretty printer provides a capability to \u201cpretty-print\u201d arbitrary Python data structures in a form which can be used as input to interpreter.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-01-12T03:30:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Pprint-Module.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Pretty Printer in Python (pprint Module)","datePublished":"2021-01-12T03:30:20+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/"},"wordCount":689,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Pprint-Module.jpg","keywords":["pretty printer in python","pretty printer module in python","Python pprint module"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/","url":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/","name":"Pretty Printer in Python (pprint Module) - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Pprint-Module.jpg","datePublished":"2021-01-12T03:30:20+00:00","description":"Pretty printer provides a capability to \u201cpretty-print\u201d arbitrary Python data structures in a form which can be used as input to interpreter.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Pprint-Module.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-Pprint-Module.jpg","width":1200,"height":628,"caption":"pretty printer in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-pprint-module\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Pretty Printer in Python (pprint Module)"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=80172"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80172\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80175"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}