{"id":4506,"date":"2022-12-27T12:43:16","date_gmt":"2022-12-27T12:43:16","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=4506"},"modified":"2022-12-27T12:43:18","modified_gmt":"2022-12-27T12:43:18","slug":"str-method-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/str-method-in-python\/","title":{"rendered":"What is an str method in Python?"},"content":{"rendered":"\n<p>In this article, we are going to understand the __str__ method in Python. This method can be said as the dunder str method, where the dunder means double underscores. These methods with double underscores are also called magic methods.<\/p>\n\n\n\n<p>So, the __str__ method returns the string representation of the object. Basically, the __str__ method is called when the functions like <strong>print<\/strong>, or <strong>str<\/strong> are invoked on the object. Other than that, we also have the __repr__ method, and if the __str__ method is not defined, then calling the print, or str function will call the __repr__ method. You can explore the __repr__ method more, but here, we are going to stick to understanding the __str__ method.<\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">What is an str method in Python?<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/str-method-in-Python.jpg\" alt=\"\" class=\"wp-image-4511\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/str-method-in-Python.jpg 640w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/str-method-in-Python-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">Default program example(without writing the __str__ method) &#8211;<\/h3>\n\n\n\n<p>Now, we are just going to dive into some practical stuff, through which, you can get the use of __str__ method. First of all, let&#8217;s have a look at a simple program, in which, we are not going to write the __str__ method, and we would try to print the object using the print function.<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">class Book:<br>&nbsp; &nbsp; def __init__(self, name, pages, price) -&gt; None:<br>&nbsp; &nbsp; &nbsp; &nbsp; self.name = name<br>&nbsp; &nbsp; &nbsp; &nbsp; self.pages = pages<br>&nbsp; &nbsp; &nbsp; &nbsp; self.price = price<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">book1 = Book(&#8220;The book1&#8221;, 100, 56)<br>print(book1)<\/p>\n\n\n\n<p>As you can see in the above program, we have a class Book, and then we also have the __init__ method. Then we are creating a Book object, and then we are trying to print it. If you try to execute the above program, you would see some weird output, which may not be what we want(It is the default representation of our object). If we are printing the Book object in the above example, we might want to have some information about the Book, and instead of that, we are getting some weird output(it is just the default representation of the objects). <\/p>\n\n\n\n<p>But let&#8217;s say that we want some information about the object when we print the object. So, we know that when we call the print function, the __str__ method is called. So, in our Book class, we just need to write the __str__ method. So, let&#8217;s do it now.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program using the __str__ method<\/h3>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">class Book:<br>&nbsp; &nbsp; def __init__(self, name, pages, price) -&gt; None:<br>&nbsp; &nbsp; &nbsp; &nbsp; self.name = name<br>&nbsp; &nbsp; &nbsp; &nbsp; self.pages = pages<br>&nbsp; &nbsp; &nbsp; &nbsp; self.price = price<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">&nbsp; &nbsp; def __str__(self) -&gt; str:<br>&nbsp; &nbsp; &nbsp; &nbsp; return f&#8221;The name of book: {self.name}nNumber of pages:{self.pages}nPrice:{self.price}&#8221;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">book1 = Book(&#8220;The book1&#8221;, 100, 56)<br>print(book1)<\/p>\n\n\n\n<p>As you can see in the above program, we just have created the __str__ method. The __str__ method returns the string, which is a kind of informal string representation of some object. After that, we created a Book object, and we are printing the object. This time, this is the output that we get &#8211;<\/p>\n\n\n\n<p><strong>Output &#8211;<\/strong><\/p>\n\n\n\n<p>The name of the book: The book1<br>Number of pages:100<br>Price:56<\/p>\n\n\n\n<p>As you can see, we got some information about the Book object this time, when we are printing the Book object.<\/p>\n\n\n\n<p>Also, we have mentioned above, that when we are using the str function, then also the __str__ method gets called. So now, let&#8217;s have an example, which tries to demonstrate the same thing.<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">class Book:<br>&nbsp; &nbsp; def __init__(self, name, pages, price) -&gt; None:<br>&nbsp; &nbsp; &nbsp; &nbsp; self.name = name<br>&nbsp; &nbsp; &nbsp; &nbsp; self.pages = pages<br>&nbsp; &nbsp; &nbsp; &nbsp; self.price = price<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">&nbsp; &nbsp; def __str__(self) -&gt; str:<br>&nbsp; &nbsp; &nbsp; &nbsp; return f&#8221;The name of book: {self.name}nNumber of pages:{self.pages}nPrice:{self.price}&#8221;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">book1 = Book(&#8220;The book1&#8221;, 100, 56)<br>info = str(book1)<br>print(info)<\/p>\n\n\n\n<p>As you can see in the above program, we just have made a small change, where we have called the str function, and then we are just printing the information. In this situation as well, we get the same output as before. Have a look at the output &#8211;<\/p>\n\n\n\n<p><strong>Output &#8211;<\/strong><\/p>\n\n\n\n<p>The name of the book: The book1<br>Number of pages:100<br>Price:56<\/p>\n\n\n\n<p>As you can see, we have got the same output this time as well. So, the __str__ method returns the string representation of the given object. This is a kind of information about the given object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Some brief about __repr__ method<\/h2>\n\n\n\n<p>Now, that we have understood much about the __str__ method, let&#8217;s discuss in brief the __repr__ method. As mentioned earlier, the __str__ method is invoked when the print\/str functions are used. If we have not written the __str__ method, then the __repr__ method is called. So, let&#8217;s define the repr method alone now, and let&#8217;s see what is the output.<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">class Book:<br>&nbsp; &nbsp; def __init__(self, name, pages, price) -&gt; None:<br>&nbsp; &nbsp; &nbsp; &nbsp; self.name = name<br>&nbsp; &nbsp; &nbsp; &nbsp; self.pages = pages<br>&nbsp; &nbsp; &nbsp; &nbsp; self.price = price<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">&nbsp; &nbsp; def __repr__(self) -&gt; str:<br>&nbsp; &nbsp; &nbsp; &nbsp; return f&#8221;Book({self.name}, {self.pages}, {self.price})&#8221;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">book1 = Book(&#8220;The book1&#8221;, 100, 56)<br>print(repr(book1))<br>print(str(book1))<br>print(book1)<\/p>\n\n\n\n<p>As you can see, this time, we have written the __repr__ method. Basically, according to the documentation, the __repr__ is used for the official string representation of an object. The thing is that if we have defined the __repr__(), but not __str__(), then the __repr__() is also used when some informal string representation of the object is required. <\/p>\n\n\n\n<p>Let&#8217;s have a look at the output of the above program.<\/p>\n\n\n\n<p><strong>Output &#8211;<\/strong> <\/p>\n\n\n\n<p>Book(The book1, 100, 56)<br>Book(The book1, 100, 56)<br>Book(The book1, 100, 56)<\/p>\n\n\n\n<p>As you can see, calling the repr(), str(), or print, results in the same output. So, this is the __repr__ method, which is used for the official string representation of an object.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p>We have understood the __str__ method, which is a dunder method in Python. It is useful when we need to have an informal string representation of our object. We can use the __str__ method with our classes and return some useful information related to the object whenever needed. We have seen some examples, to understand the __str__ method, and also we have seen the __repr__ method in brief. You can explore more about the __repr__ method.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">FAQ About str method in Python<\/h4>\n\n\n\n<div class=\"wp-block-rank-math-faq-block\"><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: What is the <strong>str<\/strong> method?<\/h3><div class=\"rank-math-answer\"><strong>Ans:<\/strong> The <strong>str<\/strong> method is used to return the string representation of the object.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: When is the <strong>str<\/strong> method called?<\/h3><div class=\"rank-math-answer\"><strong>Ans: <\/strong>The <strong>str<\/strong> method is called when the print() or str() function is invoked on an object.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: what is the <strong>repr<\/strong> method in Python?<\/h3><div class=\"rank-math-answer\"><strong>Ans: <\/strong>the <strong>repr method is used for the <\/strong>official string representation of an object. It should be a valid python expression, which can be used to create an object again.<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to understand the __str__ method in Python. This method can be said as the dunder str method, where the dunder means double underscores. These methods with double underscores are also called magic methods. So, the __str__ method returns the string representation of the object. Basically, the __str__ method is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4511,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[26,92],"class_list":{"0":"post-4506","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"tag-python","9":"tag-str-method-in-python"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=4506"}],"version-history":[{"count":1,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4506\/revisions"}],"predecessor-version":[{"id":4512,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4506\/revisions\/4512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media\/4511"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=4506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=4506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=4506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}