{"id":80185,"date":"2021-01-25T09:00:12","date_gmt":"2021-01-25T03:30:12","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80185"},"modified":"2021-01-25T09:00:12","modified_gmt":"2021-01-25T03:30:12","slug":"python-repr-function-examples","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/","title":{"rendered":"Python Repr() with Examples"},"content":{"rendered":"<p>Here we are again back with a very different and useful function in python; repr().<\/p>\n<p>And I am sure you don\u2019t wanna miss it, after all, it&#8217;s like a practice assignment without which the code will be incomplete, and I bet you won\u2019t regret using repr() in python, the more easy it is, the more frequently it is used and the more sorted it is.<\/p>\n<p>Confused? Don\u2019t worry we\u2019ll learn this together. Since the fairy basket has so much more to offer, come let&#8217;s grab repr() out of it.<\/p>\n<h2>What is Repr() in Python?<\/h2>\n<p>This string is intended to be the representation of the object, and suitable for display to the programmer, for instance, while working in interactive interpreter.<\/p>\n<p>A repr() is used in storing the data of pythonic code in such a way that it can be extracted from the code with the library function.<\/p>\n<p>A very basic code of repr is given below:<\/p>\n<p><strong>Python repr() Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class thisisrepr():\n    print(\"a conduct by repr as follow\")\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">a conduct by repr as follow<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">d = 8\n#this will print d\nprint(d)\n\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&gt;&gt; 8<\/div>\n<h3>Python repr() Parameters<\/h3>\n<p>The repr() function takes a single parameter only:<\/p>\n<p>obj &#8211; the object whose structured printable representation has to be returned.<\/p>\n<p>While the str() method produces human-readable string, we sometimes want the nitty-gritty details.<\/p>\n<p>A repr() method is a correct check on the evaluation of a runtime code. It copies the value from the library and stores it in the function which is executable.<\/p>\n<p>If a coder would like to produce a more useful result, he can override the repr() function.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import datetime \ndateandtime= datetime.datetime.now() \nprint repr(dateandtime)\n<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class TechVidvan solution:  \n    def __init__(first, second): \n       abstract.firstl = first \n       abstract.i= i\n\n    def __repr__(first): \n       return 'l(%s, %s)' % (abs.real, abs.imag)     \n\n    def __str__(first): \n       return '%s + i%s' % (p.abs, p.abs)      \nq=complex(1, 90) \n  \nprint str(q) print repr(q)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1+ i90<br \/>\nTechVidvan solution (1, 90)<\/div>\n<h3>Class Objects in Python<\/h3>\n<p>repr() method calls out the function from its internal libraries. Let\u2019s find one of our own classes.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; class age :\n       age='67'\n       def __repr__(first):\n              return repr(first.age)\n&gt;&gt;&gt; o=age()\n&gt;&gt;&gt; repr(o)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\u201c\u201867\u201d<\/div>\n<p>In this, we override the __repr__() method to get it to do what we want. However, we can\u2019t overwrite in this function, as it will then not store the correct value as an input.<\/p>\n<h3>Difference Between str() and repr() in Python<\/h3>\n<ol>\n<li>Str is used in creating a table of internal differences within the code. But at the same time repr() does it by merging the two codes from the same library.<\/li>\n<li>Str is used in the functions as a predefined library, but repr() does this by using the already existing library from the code.<\/li>\n<\/ol>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">x = str(input(\"WELCOME TO TECHVIDVAN\"))\nrepr(x)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8220;&#8216;WELCOME TO TECHVIDVAN'&#8221;<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import datetime\nnow = datetime.datetime.now()\nstr(now)\n'2020-08-09 01:30:45.200924'\nrepr(now)\n \n \n&gt;&gt;&gt; repr(now)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;datetime.datetime(2020, 9, 30, 16, 58, 56, 704204)&#8217;<\/div>\n<h3>What makes repr() a good choice?<\/h3>\n<p>Repr is easy to learn and handy to use in the programs where the coder needs to debug the codes. It is an efficient function to extract information from the predefined libraries.<\/p>\n<p>The function of repr() is well versed with the csv and data files so that it can work very well on the binary texts as well.<\/p>\n<p>More than this repr() is understandable by the user, so basically the user knows what is the accurate function to perform on his code.<\/p>\n<h2>Summary<\/h2>\n<p>Now, as we come to the end of the article, python repr() functions seem to be easy, but for a good coder, nothing is difficult or easy until it&#8217;s practiced.<\/p>\n<p>So go and start, try out yourself what is the difference between repr() and str().<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here we are again back with a very different and useful function in python; repr(). And I am sure you don\u2019t wanna miss it, after all, it&#8217;s like a practice assignment without which the&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3353,3354],"class_list":["post-80185","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-repr","tag-python-repr-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Repr() with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Understand what is repr function in Python with examples. Learn the difference between repr() and str() in Python &amp; why repr() is good choice.\" \/>\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-repr-function-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Repr() with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Understand what is repr function in Python with examples. Learn the difference between repr() and str() in Python &amp; why repr() is good choice.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/\" \/>\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-25T03:30:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-reprfunction.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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Repr() with Examples - TechVidvan","description":"Understand what is repr function in Python with examples. Learn the difference between repr() and str() in Python & why repr() is good choice.","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-repr-function-examples\/","og_locale":"en_US","og_type":"article","og_title":"Python Repr() with Examples - TechVidvan","og_description":"Understand what is repr function in Python with examples. Learn the difference between repr() and str() in Python & why repr() is good choice.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-01-25T03:30:12+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-reprfunction.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Repr() with Examples","datePublished":"2021-01-25T03:30:12+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/"},"wordCount":531,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-reprfunction.jpg","keywords":["Python repr","python repr function"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/","url":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/","name":"Python Repr() with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-reprfunction.jpg","datePublished":"2021-01-25T03:30:12+00:00","description":"Understand what is repr function in Python with examples. Learn the difference between repr() and str() in Python & why repr() is good choice.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-reprfunction.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-reprfunction.jpg","width":1200,"height":628,"caption":"Python repr() function"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-repr-function-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Repr() with Examples"}]},{"@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\/80185","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=80185"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80185\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80189"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}