{"id":1050119,"date":"2024-12-31T14:05:10","date_gmt":"2024-12-31T06:05:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1050119.html"},"modified":"2024-12-31T14:05:12","modified_gmt":"2024-12-31T06:05:12","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b0%e5%ad%97%e5%8f%98%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1050119.html","title":{"rendered":"python\u5982\u4f55\u628a\u6570\u5b57\u53d8\u4e3a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/048684a6-47d5-428d-b6ef-ae7f508dd7ea.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u628a\u6570\u5b57\u53d8\u4e3a\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528 Python \u628a\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 str()\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u4f7f\u7528 repr() \u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 str()<\/strong> \u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5185\u7f6e\u51fd\u6570 str()<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 Python \u5185\u7f6e\u7684 <code>str()<\/code> \u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002<code>str()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>num_str = str(num)<\/p>\n<p>print(num_str)  # \u8f93\u51fa: &quot;123&quot;<\/p>\n<p>print(type(num_str))  # \u8f93\u51fa: &lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a<code>str()<\/code> \u51fd\u6570\u7b80\u5355\u6613\u7528\uff0c\u5e76\u4e14\u662f\u5185\u7f6e\u51fd\u6570\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5bfc\u5165\u4efb\u4f55\u6a21\u5757\u3002\u5b83\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u662f\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\u7684\u9996\u9009\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u6574\u6570\u8fd8\u662f\u6d6e\u70b9\u6570\uff0c<code>str()<\/code> \u90fd\u80fd\u8f7b\u677e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001<code>str.format()<\/code> \u65b9\u6cd5\u548c f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0cPython 3.6 \u53ca\u4ee5\u4e0a\u7248\u672c\u5f15\u5165\uff09\u3002<\/p>\n<\/p>\n<ol>\n<li>\u767e\u5206\u53f7\u683c\u5f0f\u5316\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>num_str = &quot;%d&quot; % num<\/p>\n<p>print(num_str)  # \u8f93\u51fa: &quot;123&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><code>str.format()<\/code> \u65b9\u6cd5\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>num_str = &quot;{}&quot;.format(num)<\/p>\n<p>print(num_str)  # \u8f93\u51fa: &quot;123&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>f-string\uff08\u63a8\u8350\uff09\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>num_str = f&quot;{num}&quot;<\/p>\n<p>print(num_str)  # \u8f93\u51fa: &quot;123&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd8\u80fd\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u53d8\u91cf\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u573a\u666f\u3002\u5c24\u5176\u662f f-string\uff0c\u8bed\u6cd5\u7b80\u6d01\uff0c\u6267\u884c\u901f\u5ea6\u5feb\uff0c\u662f Python 3.6 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u63a8\u8350\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001repr() \u51fd\u6570<\/p>\n<\/p>\n<p><p><code>repr()<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u901a\u5e38\u7528\u4e8e\u8c03\u8bd5\u548c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>num_str = repr(num)<\/p>\n<p>print(num_str)  # \u8f93\u51fa: &quot;123&quot;<\/p>\n<p>print(type(num_str))  # \u8f93\u51fa: &lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a<code>repr()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u8868\u793a\u66f4\u9002\u5408\u4e8e\u5f00\u53d1\u8c03\u8bd5\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u4ee5\u7528\u6765\u91cd\u65b0\u521b\u5efa\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u867d\u7136\u5728\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\u4e0d\u5982 <code>str()<\/code> \u5e38\u7528\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e2d\u53ef\u80fd\u4f1a\u7528\u5230\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u8f6c\u6362\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u89c1\u7684\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e0d\u592a\u5e38\u89c1\u4f46\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u6570\u5b57\u5230\u5b57\u7b26\u4e32\u8f6c\u6362\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u3002\u867d\u7136\u8fd9\u4e9b\u65b9\u6cd5\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u4e0d\u5e38\u7528\uff0c\u4f46\u4e86\u89e3\u5b83\u4eec\u6709\u52a9\u4e8e\u66f4\u5168\u9762\u5730\u638c\u63e1 Python \u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>num = 123<\/p>\n<p>num_str = json.dumps(num)<\/p>\n<p>print(num_str)  # \u8f93\u51fa: &quot;123&quot;<\/p>\n<p>print(type(num_str))  # \u8f93\u51fa: &lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a<code>json.dumps()<\/code> \u65b9\u6cd5\u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5c06\u6570\u636e\u5e8f\u5217\u5316\u4e3a JSON \u683c\u5f0f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684 <code>str()<\/code> \u51fd\u6570\u3002<strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\uff08\u5c24\u5176\u662f f-string\uff09\u5728\u9700\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\u975e\u5e38\u6709\u7528\uff0c\u800c <code>repr()<\/code> \u51fd\u6570\u66f4\u9002\u5408\u5f00\u53d1\u8c03\u8bd5\u3002<strong>\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u6211\u4eec\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u7075\u6d3b\u5e94\u7528\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>Python \u7684\u5f3a\u5927\u548c\u7075\u6d3b\u6027\u4f53\u73b0\u5728\u591a\u4e2a\u65b9\u9762\uff0c\u6570\u5b57\u5230\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\u53ea\u662f\u5176\u4e2d\u4e00\u4e2a\u5c0f\u4f8b\u5b50\u3002\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1 Python\uff0c\u89e3\u51b3\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u5404\u79cd\u95ee\u9898\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>str()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u60a8\u60f3\u8981\u8f6c\u6362\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff1a<code>number = 123<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>string_number = str(number)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u9700\u8981\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u8be5\u503c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u65e0\u8bba\u662f\u6574\u6570\u8fd8\u662f\u6d6e\u70b9\u6570\uff0c\u60a8\u90fd\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u6d6e\u70b9\u6570<code>float_number = 123.45<\/code>\uff0c\u901a\u8fc7<code>string_float = str(float_number)<\/code>\uff0c\u5373\u53ef\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u548c\u663e\u793a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982<code>f-strings<\/code>\uff08\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u53ef\u7528\uff09\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5199\u6210<code>number = 123<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>formatted_string = f&quot;{number}&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u53d8\u91cf\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528 Python \u628a\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 str()\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u4f7f\u7528 repr [&hellip;]","protected":false},"author":3,"featured_media":1050123,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1050119"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1050119"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1050119\/revisions"}],"predecessor-version":[{"id":1050126,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1050119\/revisions\/1050126"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1050123"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1050119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1050119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1050119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}