Cách sử dụng phương thức __str__() và __repr__() trong Python
29/07/2025
Về tác giả
I’m passionate about web development and sharing my insights through articles, with over 8 years of experience. I hope these sharings inspire you and help build a strong web development community.
@#@
Tôi đam mê phát triển web và chia sẻ những hiểu biết của mình thông qua các bài viết, với hơn 8 năm kinh nghiệm. Tôi hy vọng những chia sẻ này sẽ truyền cảm hứng cho các bạn và giúp xây dựng một cộng đồng phát triển web mạnh mẽ.
Cập nhật thông tin mới nhấtNhận các thông tin mới nhất về mối đe dọa, báo cáo an ninh mạng từ CyStack về hòm thư điện tử của bạn
{"success":true,"head":"<title>Cách sử dụng phương thức __str__() và __repr__() trong Python - CyStack Tutorial</title>\n<meta name=\"description\" content=\"Phương thức __str__() trả về một chuỗi biểu diễn của một đối tượng mà con người có thể đọc được hay còn gọi là chuỗi biểu diễn không chính thức của một đối\"/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large\"/>\n<link rel=\"canonical\" href=\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/\" />\n<meta property=\"og:locale\" content=\"en_US\" />\n<meta property=\"og:type\" content=\"article\" />\n<meta property=\"og:title\" content=\"Cách sử dụng phương thức __str__() và __repr__() trong Python - CyStack Tutorial\" />\n<meta property=\"og:description\" content=\"Phương thức __str__() trả về một chuỗi biểu diễn của một đối tượng mà con người có thể đọc được hay còn gọi là chuỗi biểu diễn không chính thức của một đối\" />\n<meta property=\"og:url\" content=\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/\" />\n<meta property=\"og:site_name\" content=\"CyStack Tutorial\" />\n<meta property=\"article:tag\" content=\"vi\" />\n<meta property=\"article:section\" content=\"Chuyên gia\" />\n<meta property=\"og:updated_time\" content=\"2025-08-03T15:29:10+07:00\" />\n<meta property=\"og:image\" content=\"https://s2.cystack.net/tutorial/29114608/phuong-thuc-__str__.jpg\" />\n<meta property=\"og:image:secure_url\" content=\"https://s2.cystack.net/tutorial/29114608/phuong-thuc-__str__.jpg\" />\n<meta property=\"og:image:width\" content=\"1200\" />\n<meta property=\"og:image:height\" content=\"630\" />\n<meta property=\"og:image:alt\" content=\"phương thức __str__() và __repr__()\" />\n<meta property=\"og:image:type\" content=\"image/jpeg\" />\n<meta property=\"article:published_time\" content=\"2025-07-29T11:48:04+07:00\" />\n<meta property=\"article:modified_time\" content=\"2025-08-03T15:29:10+07:00\" />\n<meta name=\"twitter:card\" content=\"summary_large_image\" />\n<meta name=\"twitter:title\" content=\"Cách sử dụng phương thức __str__() và __repr__() trong Python - CyStack Tutorial\" />\n<meta name=\"twitter:description\" content=\"Phương thức __str__() trả về một chuỗi biểu diễn của một đối tượng mà con người có thể đọc được hay còn gọi là chuỗi biểu diễn không chính thức của một đối\" />\n<meta name=\"twitter:image\" content=\"https://s2.cystack.net/tutorial/29114608/phuong-thuc-__str__.jpg\" />\n<meta name=\"twitter:label1\" content=\"Written by\" />\n<meta name=\"twitter:data1\" content=\"Bao Tran\" />\n<meta name=\"twitter:label2\" content=\"Time to read\" />\n<meta name=\"twitter:data2\" content=\"5 minutes\" />\n<script type=\"application/ld+json\" class=\"rank-math-schema\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https://blog.cystack.org/tutorial/#person\",\"name\":\"CyStack Tutorial\"},{\"@type\":\"WebSite\",\"@id\":\"https://blog.cystack.org/tutorial/#website\",\"url\":\"https://blog.cystack.org/tutorial\",\"name\":\"CyStack Tutorial\",\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https://s2.cystack.net/tutorial/29114608/phuong-thuc-__str__.jpg\",\"url\":\"https://s2.cystack.net/tutorial/29114608/phuong-thuc-__str__.jpg\",\"width\":\"1200\",\"height\":\"630\",\"caption\":\"ph\\u01b0\\u01a1ng th\\u1ee9c __str__() v\\u00e0 __repr__()\",\"inLanguage\":\"en-US\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":\"1\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":\"2\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/category/chuyen-gia/\",\"name\":\"Chuy\\u00ean gia\"}},{\"@type\":\"ListItem\",\"position\":\"3\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/\",\"name\":\"C\\u00e1ch s\\u1eed d\\u1ee5ng ph\\u01b0\\u01a1ng th\\u1ee9c __str__() v\\u00e0 __repr__() trong Python\"}}]},{\"@type\":\"WebPage\",\"@id\":\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/#webpage\",\"url\":\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/\",\"name\":\"C\\u00e1ch s\\u1eed d\\u1ee5ng ph\\u01b0\\u01a1ng th\\u1ee9c __str__() v\\u00e0 __repr__() trong Python - CyStack Tutorial\",\"datePublished\":\"2025-07-29T11:48:04+07:00\",\"dateModified\":\"2025-08-03T15:29:10+07:00\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/#website\"},\"primaryImageOfPage\":{\"@id\":\"https://s2.cystack.net/tutorial/29114608/phuong-thuc-__str__.jpg\"},\"inLanguage\":\"en-US\",\"breadcrumb\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/#breadcrumb\"}},{\"@type\":\"Person\",\"@id\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"name\":\"Bao Tran\",\"url\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https://secure.gravatar.com/avatar/0cdd33c02ec4a531fcf557b9c1ccc276df0c69031b2382c959bcf335248e840c?s=96&d=mm&r=g\",\"url\":\"https://secure.gravatar.com/avatar/0cdd33c02ec4a531fcf557b9c1ccc276df0c69031b2382c959bcf335248e840c?s=96&d=mm&r=g\",\"caption\":\"Bao Tran\",\"inLanguage\":\"en-US\"}},{\"@type\":\"BlogPosting\",\"headline\":\"C\\u00e1ch s\\u1eed d\\u1ee5ng ph\\u01b0\\u01a1ng th\\u1ee9c __str__() v\\u00e0 __repr__() trong Python - CyStack Tutorial\",\"keywords\":\"__str__()\",\"datePublished\":\"2025-07-29T11:48:04+07:00\",\"dateModified\":\"2025-08-03T15:29:10+07:00\",\"articleSection\":\"Chuy\\u00ean gia\",\"author\":{\"@id\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"name\":\"Bao Tran\"},\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"description\":\"Ph\\u01b0\\u01a1ng th\\u1ee9c __str__() tr\\u1ea3 v\\u1ec1 m\\u1ed9t chu\\u1ed7i bi\\u1ec3u di\\u1ec5n c\\u1ee7a m\\u1ed9t \\u0111\\u1ed1i t\\u01b0\\u1ee3ng m\\u00e0 con ng\\u01b0\\u1eddi c\\u00f3 th\\u1ec3 \\u0111\\u1ecdc \\u0111\\u01b0\\u1ee3c hay c\\u00f2n g\\u1ecdi l\\u00e0 chu\\u1ed7i bi\\u1ec3u di\\u1ec5n kh\\u00f4ng ch\\u00ednh th\\u1ee9c c\\u1ee7a m\\u1ed9t \\u0111\\u1ed1i\",\"name\":\"C\\u00e1ch s\\u1eed d\\u1ee5ng ph\\u01b0\\u01a1ng th\\u1ee9c __str__() v\\u00e0 __repr__() trong Python - CyStack Tutorial\",\"@id\":\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/#richSnippet\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/#webpage\"},\"image\":{\"@id\":\"https://s2.cystack.net/tutorial/29114608/phuong-thuc-__str__.jpg\"},\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/07/29/phuong-thuc-str-va-repr/#webpage\"}}]}</script>\n"}