PageSourceSearch

https://www.tutorialsteacher.com/_next/static/chunks/pages/articles/repr-vs-str-in-python-c0d108508076767e.js

js tutorialsteacher.com collected 2026-10-02 04:13:13 UTC 10,863 bytes, 1 lines download raw bytes

1(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[74027],{32589:function(e,s,a){(window.__NEXT_P=window.__NEXT_P||[]).push(["/articles/repr-vs-str-in-python",function(){return a(53871)}])},53871:function(e,s,a){"use strict";a.r(s);var r=a(85893),n=a(9008),c=a.n(n);s.default=()=>(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(c(),{children:[(0,r.jsx)("title",{children:"repr() vs str() in Python"}),(0,r.jsx)("meta",{property:"og:title",content:"repr() vs str() in Python"}),(0,r.jsx)("meta",{name:"keywords",content:"repr, str, python"}),(0,r.jsx)("meta",{name:"description",content:"The <code>object</code> class is the base class for all classes, has a number of dunder (short for double underscore) methods. "})]}),(0,r.jsxs)("article",{children:[(0,r.jsx)("h1",{children:"repr() vs str() in Python"}),(0,r.jsxs)("p",{children:["The ",(0,r.jsx)("code",{children:"object"})," class is the base class for all classes, has a number of dunder (short for double underscore) methods. They are often called ",(0,r.jsx)("a",{href:"/python/magic-methods-in-python",target:"_blank",className:"external-link",children:"magic methods"}),". These methods are never called directly. Instead, a corresponding built-in function internally calls one of these magic methods. For instance built-in ",(0,r.jsx)("a",{href:"/python/str-method",target:"_blank",className:"external-link",children:"str()"})," function invokes ",(0,r.jsx)("code",{children:"__str__()"})," method and ",(0,r.jsx)("a",{href:"/python/repr-method",target:"_blank",className:"external-link",children:"repr()"})," function invokes ",(0,r.jsx)("code",{children:"__repr__()"})," method."]}),(0,r.jsxs)("div",{className:"card code-panel",children:[(0,r.jsxs)("div",{className:"card-header example-header",children:[(0,r.jsx)("div",{className:"example-caption",children:"Example: str() and __str__() method on int object"}),(0,r.jsxs)("button",{className:"copy-btn pull-right",title:"Copy example code",children:[(0,r.jsx)("i",{className:"fa fa-copy"})," Copy"]})]}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"language-python",children:(0,r.jsx)("code",{children:">>> x=100 >>> print(str(x)) 100 >>> print(x.__str__()) 100"})})})]}),(0,r.jsxs)("p",{children:["The following invokes the ",(0,r.jsx)("code",{children:"repr()"})," and ",(0,r.jsx)("code",{children:"__repr__()"})," method:"]}),(0,r.jsxs)("div",{className:"card code-panel",children:[(0,r.jsxs)("div",{className:"card-header example-header",children:[(0,r.jsx)("div",{className:"example-caption",children:"Example: repr() and __repr__() method on int object"}),(0,r.jsxs)("button",{className:"copy-btn pull-right",title:"Copy example code",children:[(0,r.jsx)("i",{className:"fa fa-copy"})," Copy"]})]}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"language-python",children:(0,r.jsx)("code",{children:">>> x=100 >>> print(repr(x)) 100 >>> print(x.__repr__()) 100"})})})]}),(0,r.jsxs)("p",{children:["Apparently, there seems to be no difference between behavior of the ",(0,r.jsx)("code",{children:"__str__()"})," and ",(0,r.jsx)("code",{children:"__repr__()"}),". However, if we take a string object, the difference will be evident."]}),(0,r.jsxs)("div",{className:"card code-panel",children:[(0,r.jsxs)("div",{className:"card-header example-header",children:[(0,r.jsx)("div",{className:"example-caption",children:"Example: __repr__() vs __str__()"}),(0,r.jsxs)("button",{className:"copy-btn pull-right",title:"Copy example code",children:[(0,r.jsx)("i",{className:"fa fa-copy"})," Copy"]})]}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"language-python",children:(0,r.jsx)("code",{children:">>> x=\"Hello World\" >>> print(x.__str__()) Hello World >>> print(x.__repr__()) 'Hello World'"})})})]}),(0,r.jsxs)("p",{children:["Output of  ",(0,r.jsx)("code",{children:"__repr__()"})," is in quotes whereas that of ",(0,r.jsx)("code",{children:"__str__()"})," is not. The reason can be traced to official definitions of these functions, which says that ",(0,r.jsx)("code",{children:"__repr__()"})," method and hence (repr() function) computes ",(0,r.jsx)("strong",{children:"official"})," string representation of an object. The str() function i.e. ",(0,r.jsx)("code",{children:"__str__()"})," method returns an ",(0,r.jsx)("strong",{children:"informal"}
1)," or printable string representation of concerned object, which is used by the ",(0,r.jsx)("code",{children:"print()"})," and ",(0,r.jsx)("code",{children:"format()"})," functions."]}),(0,r.jsxs)("p",{children:["If you try to use an object of any user-defined class as an argument for the ",(0,r.jsx)("code",{children:"print()"})," function, it uses the default ",(0,r.jsx)("code",{children:"__str__()"})," method of object's class. It doesn't tell any meaningful info of the object other than its id."]}),(0,r.jsxs)("div",{className:"card code-panel",children:[(0,r.jsxs)("div",{className:"card-header example-header",children:[(0,r.jsx)("div",{className:"example-caption",children:"Example: Print an Object"}),(0,r.jsxs)("button",{className:"copy-btn pull-right",title:"Copy example code",children:[(0,r.jsx)("i",{className:"fa fa-copy"})," Copy"]})]}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"language-python",children:(0,r.jsx)("code",{children:'class myclass: def __init__(self): self.name="Raj" self.age=21 obj = myclass() print(obj)'})})})]}),(0,r.jsxs)("div",{className:"card output-panel",children:[(0,r.jsx)("div",{className:"card-header  example-header",children:(0,r.jsx)("div",{className:"output-caption",children:"Output"})}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"csharpcode",children:(0,r.jsx)("code",{children:"<__main__.myclass object at 0x000001E06B3ADE08>"})})})]}),(0,r.jsxs)("p",{children:["Whereas, we would like to obtain string representation showing values of object attributes. This can be done by overriding the ",(0,r.jsx)("code",{children:"__str__()"})," method in the ",(0,r.jsx)("code",{children:"myclass"}),", as shown below."]}),(0,r.jsxs)("div",{className:"card code-panel",children:[(0,r.jsxs)("div",{className:"card-header example-header",children:[(0,r.jsx)("div",{className:"example-caption",children:"Example: Override __str__() Method "}),(0,r.jsxs)("button",{className:"copy-btn pull-right",title:"Copy example code",children:[(0,r.jsx)("i",{className:"fa fa-copy"})," Copy"]})]}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"language-python",children:(0,r.jsxs)("code",{children:['class myclass: def __init__(self): self.name="Raj" self.age=21 def __str__(self): return "name : '," age : ",'".format(self.name, self.age) obj=myclass() print(obj) print(str(obj))']})})})]}),(0,r.jsxs)("div",{className:"card output-panel",children:[(0,r.jsx)("div",{className:"card-header  example-header",children:(0,r.jsx)("div",{className:"output-caption",children:"Output"})}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"csharpcode",children:(0,r.jsx)("code",{children:"name : Raj age : 21 name : Raj age : 21"})})})]}),(0,r.jsxs)("p",{children:["Note that str() function has used ",(0,r.jsx)("code",{children:"__str__()"})," method in the class. The ",(0,r.jsx)("code",{children:"__repr__()"})," method can also be used for same purpose. However, the str() function by default uses ",(0,r.jsx)("code",{children:"__str__()"}),", if not found uses ",(0,r.jsx)("code",{children:"__repr__()"}),"."]}),(0,r.jsxs)("p",{children:["Another difference between the two is, ",(0,r.jsx)("code",{children:"__str__()"})," should always return a string, whereas the ",(0,r.jsx)("code",{children:"__repr__()"})," can return any valid Python expression. Let us add both methods in myclass so that the ",(0,r.jsx)("code",{children:"__repr__()"})," returns a dict object."]}),(0,r.jsxs)("div",{className:"card code-panel",children:[(0,r.jsxs)("div",{className:"card-header example-header",children:[(0,r.jsx)("div",{className:"example-caption",children:"Example: Override __str__() and __repr__() "}),(0,r.jsxs)("button",{className:"copy-btn pull-right",title:"Copy example code",children:[(0,r.jsx)("i",{className:"fa fa-copy"})," Copy"]})]}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"language-python",children:(0,r.jsxs)("code",{children:['class myclass: def __init__(self): self.name="Raj" self.age=21 def __str__(self): return "name : '," age : ",'".format(self.name, self.age) def __repr__(self): return ','{"name":self.name, "age":self.age}',"obj = myclass() print(obj.__str__()) print(obj.__repr__())"]})})})]}),(0,r.jsxs)("div",{className:"card output-panel",children:[(0,r.jsx)("div",{className:"card-header  example-header",children:(0,r.jsx)("div",{className:"output-caption",children:"Output"})}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"csharpcode",children:(0,r.jsxs)("code",{children:["name : Raj age : 21","{'name': 'Raj', 'age': 21}"]})})})]}),(0,r.jsxs)("p",{children:["Now, this will bring out exact difference between the two. While ",(0,r.jsx)("code",{children:"str()"})," correctly implements the ",(0,r.jsx)("code",{children:"__str__()"}),", repr() function throws the ",(0,r.jsx)("code",{children:"TypeError"})," as it needs ",(0,r.jsx)("code",{children:"__repr__()"})," toreturn a string."]}),(0,r.jsxs)("div",{className:"card code-panel",children:[(0,r.jsxs)("div",{className:"card-header example-header",children:[(0,r.jsx)("div",{className:"example-caption",children:"Example: repr() vs str() "}),(0,r.jsxs)("button",{className:"copy-btn pull-right",title:"Copy example code",children:[(0,r.jsx)("i",{className:"fa fa-copy"})," Copy"]})]}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"language-python",children:(0,r.jsx)("code",{children:"print(str(obj)) print(repr(obj))"})})})]}),(0,r.jsxs)("div",{className:"card output-panel",children:[(0,r.jsx)("div",{className:"card-header  example-header",children:(0,r.jsx)("div",{className:"output-caption",children:"Output"})}),(0,r.jsx)("div",{className:"panel-body",children:(0,r.jsx)("pre",{className:"csharpcode",children:(0,r.jsx)("code",{children:"name : Raj age : 21 TypeError: __repr__ returned non-string (type dict)"})})})]}),(0,r.jsxs)("p",{children:["The output of ",(0,r.jsx)("code",{children:"__repr__()"})," method is mostly machine-readable and is mainly used for debugging purposes. He
1nce it is desired to be furnishing unambiguous representation of the object."]}),(0,r.jsxs)("p",{children:["Purpose of ",(0,r.jsx)("code",{children:"__str__()"})," is to generate a printable string representation that is informal in nature and human-readable in nature."]})]})]})}},function(e){e.O(0,[92888,49774,40179],function(){return e(e.s=32589)}),_N_E=e.O()}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.