{"id":1055040,"date":"2024-12-31T14:47:14","date_gmt":"2024-12-31T06:47:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1055040.html"},"modified":"2024-12-31T14:47:16","modified_gmt":"2024-12-31T06:47:16","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8a%8aint%e8%bd%ac%e6%8d%a2%e4%b8%bastr","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1055040.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u628aint\u8f6c\u6362\u4e3astr"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2a47eaf3-b111-45c6-bc4f-057280b3cbd1.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u628aint\u8f6c\u6362\u4e3astr\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0cint\u7c7b\u578b\u8f6c\u6362\u4e3astr\u7c7b\u578b\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528str()\u51fd\u6570\u3001f\u5b57\u7b26\u4e32\u3001repr()\u51fd\u6570\u3001%\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26\u3001format()\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u3002\u8ba9\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e00\u4e0b\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528str()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>str()<\/code>\u51fd\u6570\u662f\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u3002\u5b83\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5c06\u6574\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>str()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u793a\u4f8b\uff1a<\/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\uff1a&#39;123&#39;<\/p>\n<p>print(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u7684\u4f18\u70b9\u662f\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u5c06\u5176\u4ed6\u7c7b\u578b\uff08\u5982\u6d6e\u70b9\u6570\u3001\u5e03\u5c14\u503c\u7b49\uff09\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528f\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>Python 3.6\u5f15\u5165\u4e86f\u5b57\u7b26\u4e32\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\uff0c\u5b83\u662f\u4e00\u79cd\u66f4\u52a0\u7b80\u6d01\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u4f7f\u7528f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u5e76\u81ea\u52a8\u8c03\u7528<code>str()<\/code>\u51fd\u6570\u5c06\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\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\uff1a&#39;123&#39;<\/p>\n<p>print(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f\u5b57\u7b26\u4e32\u7684\u4f18\u70b9\u662f\u8bed\u6cd5\u7b80\u6d01\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u591a\u4e2a\u53d8\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528repr()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>repr()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u201c\u5b98\u65b9\u201d\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7<code>eval()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u56de\u539f\u59cb\u5bf9\u8c61\u3002\u5bf9\u4e8e\u6574\u6570\uff0c<code>repr()<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e0e<code>str()<\/code>\u51fd\u6570\u76f8\u540c\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\uff1a&#39;123&#39;<\/p>\n<p>print(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>repr()<\/code>\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u8c03\u8bd5\u548c\u8bb0\u5f55\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528%\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26<\/p>\n<\/p>\n<p><p><code>%<\/code>\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26\u662fPython\u4e2d\u4e00\u79cd\u65e7\u5f0f\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u4e2d\u7684<code>printf<\/code>\u3002\u5b83\u53ef\u4ee5\u5c06\u6574\u6570\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\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\uff1a&#39;123&#39;<\/p>\n<p>print(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u5728Python 3.x\u4e2d\u5df2\u7ecf\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u4f46\u5728\u67d0\u4e9b\u9057\u7559\u4ee3\u7801\u4e2d\u4ecd\u7136\u53ef\u4ee5\u89c1\u5230\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528format()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>format()<\/code>\u65b9\u6cd5\u662fPython 2.7\u548c3.0\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u8c03\u7528\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684<code>format()<\/code>\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\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\uff1a&#39;123&#39;<\/p>\n<p>print(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>format()<\/code>\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u5c06int\u7c7b\u578b\u8f6c\u6362\u4e3astr\u7c7b\u578b\u7684\u65b9\u6cd5\u4e3b\u8981\u6709str()\u51fd\u6570\u3001f\u5b57\u7b26\u4e32\u3001repr()\u51fd\u6570\u3001%\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26\u3001format()\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f<code>str()<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u5982\u679c\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u591a\u4e2a\u53d8\u91cf\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528f\u5b57\u7b26\u4e32\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8be6\u7ec6\u4ecb\u7ecd\u548c\u793a\u4f8b\uff0c\u4f60\u5e94\u8be5\u5bf9Python\u4e2d\u5c06int\u7c7b\u578b\u8f6c\u6362\u4e3astr\u7c7b\u578b\u7684\u65b9\u6cd5\u6709\u4e86\u5168\u9762\u7684\u4e86\u89e3\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff0c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>str()<\/code>\u51fd\u6570\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>str(123)<\/code>\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32<code>&#39;123&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u6574\u6570\u7c7b\u578b\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06int\u8f6c\u6362\u4e3astr\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u65b9\u6cd5\u6765\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<code>f&quot;{num}&quot;<\/code>\u4e5f\u80fd\u5b9e\u73b0\u5c06\u6574\u6570<code>num<\/code>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u9700\u8981\u540c\u65f6\u5305\u542b\u53d8\u91cf\u548c\u5176\u4ed6\u6587\u672c\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4f1a\u4e22\u5931\u4fe1\u606f\u5417\uff1f<\/strong><br \/>\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u4e0d\u4f1a\u4e22\u5931\u4fe1\u606f\u3002\u6574\u6570\u7684\u503c\u4f1a\u5b8c\u6574\u4fdd\u7559\uff0c\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u4f1a\u7cbe\u786e\u53cd\u6620\u51fa\u539f\u59cb\u6574\u6570\u7684\u5185\u5bb9\u3002\u65e0\u8bba\u662f\u6b63\u6574\u6570\u3001\u8d1f\u6574\u6570\u8fd8\u662f\u96f6\uff0c\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u90fd\u662f\u51c6\u786e\u7684\u8868\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u518d\u8f6c\u6362\u56de\u6574\u6570\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>int(&#39;123&#39;)<\/code>\u5c06\u8fd4\u56de\u6574\u6570<code>123<\/code>\u3002\u5728\u8f6c\u6362\u65f6\uff0c\u8bf7\u786e\u4fdd\u5b57\u7b26\u4e32\u5185\u5bb9\u662f\u6709\u6548\u7684\u6574\u6570\u683c\u5f0f\uff0c\u5426\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cint\u7c7b\u578b\u8f6c\u6362\u4e3astr\u7c7b\u578b\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528str()\u51fd\u6570\u3001f\u5b57\u7b26\u4e32\u3001repr()\u51fd\u6570\u3001%\u683c [&hellip;]","protected":false},"author":3,"featured_media":1055044,"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\/1055040"}],"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=1055040"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1055040\/revisions"}],"predecessor-version":[{"id":1055045,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1055040\/revisions\/1055045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1055044"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1055040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1055040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1055040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}