{"id":151,"date":"2024-01-25T13:49:09","date_gmt":"2024-01-25T13:49:09","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=151"},"modified":"2024-01-25T13:49:25","modified_gmt":"2024-01-25T13:49:25","slug":"python-__str__","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-__str__\/","title":{"rendered":"Python __str__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>__str__<\/code>&nbsp;method to make a string representation of a class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python&nbsp;<code>__str__<\/code>&nbsp;method<\/h2>\n\n\n\n<p>Let\u2019s start with the\u00a0<code>Person<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Person<\/code>\u00a0class has three\u00a0instance attributes\u00a0including\u00a0<code>first_name<\/code>,\u00a0<code>last_name<\/code>, and\u00a0<code>age<\/code>.<\/p>\n\n\n\n<p>The following creates a new instance of the\u00a0<code>Person<\/code>\u00a0class and display it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person('John', 'Doe', 25) print(person)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;__main__.Person object at 0x0000023CA16D13A0><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When you use the&nbsp;<code>print()<\/code>&nbsp;function to display the instance of the&nbsp;<code>Person<\/code>&nbsp;class, the&nbsp;<code>print()<\/code>&nbsp;function shows the memory address of that instance.<\/p>\n\n\n\n<p>Sometimes, it\u2019s useful to have a string representation of an instance of a class. To customize the string representation of a class instance, the class needs to implement the&nbsp;<code>__str__<\/code>&nbsp;magic method.<\/p>\n\n\n\n<p>Internally, Python will call the&nbsp;<code>__str__<\/code>&nbsp;method automatically when an instance calls the&nbsp;<code>str()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>Note that the&nbsp;<code>print()<\/code>&nbsp;function converts all non-keyword arguments to strings by passing them to the&nbsp;<code>str()<\/code>&nbsp;before displaying the string values.<\/p>\n\n\n\n<p>The following illustrates how to implement the\u00a0<code>__str__<\/code>\u00a0method in the\u00a0<code>Person<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __str__(self): return f'Person({self.first_name},{self.last_name},{self.age})'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And when you use the\u00a0<code>print()<\/code>\u00a0function to print out an instance of the\u00a0<code>Person<\/code>\u00a0class, Python calls the\u00a0<code>__str__<\/code>\u00a0method defined in the\u00a0<code>Person<\/code>\u00a0class. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person('John', 'Doe', 25) print(person)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Person(John,Doe,25)<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;__str__&nbsp;method to make a string representation of a class. Introduction to the Python&nbsp;__str__&nbsp;method Let\u2019s start with the\u00a0Person\u00a0class: The\u00a0Person\u00a0class has three\u00a0instance attributes\u00a0including\u00a0first_name,\u00a0last_name, and\u00a0age. The following creates a new instance of the\u00a0Person\u00a0class and display it: Output: When you use the&nbsp;print()&nbsp;function to display the instance of the&nbsp;Person&nbsp;class, the&nbsp;print()&nbsp;function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-2-special-methods"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/151\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}