{"id":2892,"date":"2025-05-30T16:49:14","date_gmt":"2025-06-04T09:28:13","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2892"},"modified":"2025-06-04T16:49:14","modified_gmt":"2025-06-04T09:28:13","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84str%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84str%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 str() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, str()\"><\/p>\n<p>Python \u7684 str() \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5167\u5efa\u51fd\u6578\uff0c\u5b83\u80fd\u5920\u5c07\u4efb\u4f55\u985e\u578b\u7684\u8cc7\u6599\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u3002\u9019\u5c0d\u65bc\u6578\u64da\u5448\u73fe\u548c\u683c\u5f0f\u5316\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u5c07\u8a73\u7d30\u4ecb\u7d39 str() \u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u4ee5\u53ca\u683c\u5f0f\u5316\u6280\u5de7\uff0c\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u904b\u7528\u9019\u500b\u51fd\u6578\u3002<\/p>\n<h2>str() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>str() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c07\u4efb\u4f55\u985e\u578b\u7684\u8cc7\u6599\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f7f\u7528\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\n# \u5c07\u6578\u5b57\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\nx = str(123)\nprint(x)\n# \u8f38\u51fa\uff1a'123'\n\n# \u5c07\u5217\u8868\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\ny = str([1, 2, 3])\nprint(y)\n# \u8f38\u51fa\uff1a'[1, 2, 3]'\n\n# \u5c07\u5143\u7d44\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\nz = str((1, 2, 3))\nprint(z)\n# \u8f38\u51fa\uff1a'(1, 2, 3)'\n\n# \u5c07\u5b57\u5178\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\na = str({'name': 'John', 'age': 20})\nprint(a)\n# \u8f38\u51fa\uff1a\"{'name': 'John', 'age': 20}\"\n<\/pre>\n<h2>str() \u51fd\u6578\u7684\u683c\u5f0f\u5316\u7528\u6cd5<\/h2>\n<p>str() \u51fd\u6578\u4e5f\u53ef\u4ee5\u7528\u65bc\u683c\u5f0f\u5316\u6578\u5b57\u3002\u5f9e Python 3.6 \u958b\u59cb\uff0c\u63a8\u85a6\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u4f86\u9032\u884c\u66f4\u9748\u6d3b\u7684\u683c\u5f0f\u5316\u3002\u4f46\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528 str() \u51fd\u6578\u4f86\u6307\u5b9a\u8f49\u63db\u7684\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\n# \u5c07\u6578\u5b57\u8f49\u63db\u70ba\u6307\u5b9a\u7684\u683c\u5f0f\nx = str(123.4567)\nprint(x)\n# \u8f38\u51fa\uff1a'123.4567'\n\n# \u4f7f\u7528 f-string \u9032\u884c\u6578\u5b57\u683c\u5f0f\u5316\ny = f\"{123.4567:.2f}\"\nprint(y)\n# \u8f38\u51fa\uff1a'123.46'\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 str() \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **\u985e\u578b\u932f\u8aa4**\uff1a\u5982\u679c\u50b3\u5165\u7684\u53c3\u6578\u70ba\u81ea\u5b9a\u7fa9\u5c0d\u8c61\uff0c\u78ba\u4fdd\u8a72\u5c0d\u8c61\u5be6\u73fe\u4e86 __str__ \u65b9\u6cd5\uff0c\u5426\u5247\u5c07\u6703\u5f15\u767c\u932f\u8aa4\u3002<br \/>\n2. **\u683c\u5f0f\u5316\u932f\u8aa4**\uff1a\u5982\u679c\u4f7f\u7528\u683c\u5f0f\u5316\u9078\u9805\uff0c\u8acb\u78ba\u4fdd\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u8a9e\u6cd5\u6b63\u78ba\uff0c\u5426\u5247\u5c07\u6703\u5f15\u767c ValueError\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>str() \u51fd\u6578\u5728\u8a31\u591a\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u3001\u7db2\u7d61\u901a\u4fe1\u4ee5\u53ca\u65e5\u8a8c\u8a18\u9304\u7b49\u65b9\u9762\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4f86\u5c07\u975e\u5b57\u7b26\u4e32\u6578\u64da\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fbf\u65bc\u6578\u64da\u7684\u5b58\u5132\u548c\u50b3\u8f38\u3002<\/p>\n<p>\u82e5\u60f3\u6df1\u5165\u4e86\u89e3 Python \u7684\u66f4\u591a\u529f\u80fd\uff0c\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7 [Python \u6559\u5b78\u8cc7\u6e90](https:\/\/vocus.cc) \u4ee5\u7372\u5f97\u66f4\u591a\u5be6\u7528\u8cc7\u8a0a\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 str() \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u51fd\u6578\uff0c\u5b83\u80fd\u5920\u5c07\u4efb\u4f55\u985e\u578b\u7684\u8cc7\u6599\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4e26\u4e14\u53ef\u4ee5\u6307\u5b9a\u8f49\u63db\u7684\u683c\u5f0f\u3002\u7121\u8ad6\u662f\u6578\u5b57\u3001\u5217\u8868\u3001\u5143\u7d44\u9084\u662f\u5b57\u5178\uff0cstr() \u51fd\u6578\u90fd\u80fd\u5920\u8f15\u9b06\u8655\u7406\uff0c\u8b93\u4f60\u7684\u6578\u64da\u8655\u7406\u66f4\u52a0\u4fbf\u5229\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1\uff1astr() \u51fd\u6578\u53ef\u4ee5\u8f49\u63db\u4ec0\u9ebc\u985e\u578b\u7684\u6578\u64da\uff1f**<br \/>\nA1\uff1astr() \u51fd\u6578\u53ef\u4ee5\u8f49\u63db\u6578\u5b57\u3001\u5217\u8868\u3001\u5143\u7d44\u3001\u5b57\u5178\u4ee5\u53ca\u5176\u4ed6\u53ef\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u7684\u5c0d\u8c61\u3002<\/p>\n<p>**Q2\uff1a\u5982\u4f55\u683c\u5f0f\u5316\u6578\u5b57\u4ee5\u7279\u5b9a\u7684\u986f\u793a\u65b9\u5f0f\uff1f**<br \/>\nA2\uff1a\u53ef\u4ee5\u4f7f\u7528 Python 3.6 \u4ee5\u5f8c\u7684 f-string\uff0c\u6216\u662f\u4f7f\u7528 str.format() \u65b9\u6cd5\u4f86\u683c\u5f0f\u5316\u6578\u5b57\u3002<\/p>\n<p>**Q3\uff1a\u5982\u679c\u6211\u50b3\u905e\u4e00\u500b\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7d66 str()\uff0c\u6703\u767c\u751f\u4ec0\u9ebc\u60c5\u6cc1\uff1f**<br \/>\nA3\uff1a\u5982\u679c\u8a72\u5c0d\u8c61\u5be6\u73fe\u4e86 __str__ \u65b9\u6cd5\uff0c\u5247\u6703\u8fd4\u56de\u8a72\u65b9\u6cd5\u7684\u7d50\u679c\uff1b\u5982\u679c\u6c92\u6709\uff0c\u5247\u6703\u5f15\u767c TypeError\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>str()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u7269\u4ef6\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u64cd\u4f5c\u548c\u64cd\u7e31\u5b57\u7b26\u4e32\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39str()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2892","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-KE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2892"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2892\/revisions"}],"predecessor-version":[{"id":2893,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2892\/revisions\/2893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}