{"id":1080298,"date":"2025-01-08T12:28:47","date_gmt":"2025-01-08T04:28:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080298.html"},"modified":"2025-01-08T12:28:50","modified_gmt":"2025-01-08T04:28:50","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b4%e6%95%b0%e8%bd%ac%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080298.html","title":{"rendered":"python\u5982\u4f55\u628a\u6574\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182913\/d511f7de-ad50-4f2d-a3c3-4bc744d7fbd1.webp\" alt=\"python\u5982\u4f55\u628a\u6574\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>str()<\/code>\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f7f\u7528<code>f-string<\/code>\u7b49\u3002\u6700\u5e38\u7528\u4e14\u7b80\u6d01\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u5c06\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528 <code>str()<\/code> \u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>str()<\/code> \u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u65b9\u6cd5\u6700\u4e3a\u76f4\u63a5\u548c\u5e38\u7528\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>num_str = str(num)<\/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><strong>str() \u51fd\u6570\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\u3001\u9002\u7528\u4e8e\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5728Python\u4e2d\u6709\u591a\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528\u767e\u5206\u53f7<code>%<\/code>\u3001<code>format()<\/code>\u65b9\u6cd5\u548c<code>f-string<\/code>\u3002\u5b83\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff0c\u8fd8\u80fd\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u53d8\u91cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u767e\u5206\u53f7 <code>%<\/code><\/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(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528 <code>format()<\/code> \u65b9\u6cd5<\/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(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528 <code>f-string<\/code>\uff08Python 3.6 \u53ca\u4ee5\u4e0a\u7248\u672c\uff09<\/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(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>f-string \u662f Python 3.6 \u5f15\u5165\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b0\u8bed\u6cd5\uff0c\u5177\u6709\u66f4\u597d\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528 <code>repr()<\/code> \u51fd\u6570<\/h2>\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\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7 <code>eval()<\/code> \u51fd\u6570\u5c06\u8be5\u5b57\u7b26\u4e32\u91cd\u65b0\u8f6c\u6362\u56de\u539f\u5bf9\u8c61\u3002\u5bf9\u4e8e\u6574\u6570\uff0c<code>repr()<\/code> \u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u4e0e <code>str()<\/code> \u8fd4\u56de\u7684\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(type(num_str))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>repr() \u51fd\u6570\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u8c03\u8bd5\u548c\u8bb0\u5f55\u65e5\u5fd7\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5c3d\u7ba1 <code>join()<\/code> \u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u5b83\u4e5f\u80fd\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u8f83\u4e3a\u5c11\u89c1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>num_str = &#39;&#39;.join([str(num)])<\/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><strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u7075\u6d3b\u6027\u8f83\u4f4e\uff0c\u901a\u5e38\u4e0d\u4f1a\u88ab\u63a8\u8350\u7528\u4e8e\u7b80\u5355\u7684\u7c7b\u578b\u8f6c\u6362\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u8f83\u5c11\u4f7f\u7528\uff0c\u4f46\u5b83\u5c55\u793a\u4e86 Python \u7684\u7075\u6d3b\u6027\u4e0e\u591a\u6837\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>num_str = &#39;&#39;.join([str(i) for i in [num]])<\/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><strong>\u8fd9\u79cd\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u5c55\u793a Python \u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6027\u80fd\u53ef\u80fd\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u56e0\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>timeit<\/code> \u6a21\u5757\u5bf9\u51e0\u79cd\u65b9\u6cd5\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>num = 123<\/p>\n<h2><strong>\u4f7f\u7528 str() \u51fd\u6570<\/strong><\/h2>\n<p>print(timeit.timeit(&#39;str(num)&#39;, globals=globals(), number=1000000))<\/p>\n<h2><strong>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32 %<\/strong><\/h2>\n<p>print(timeit.timeit(&#39;&quot;%d&quot; % num&#39;, globals=globals(), number=1000000))<\/p>\n<h2><strong>\u4f7f\u7528 format() \u65b9\u6cd5<\/strong><\/h2>\n<p>print(timeit.timeit(&#39;&quot;{}&quot;.format(num)&#39;, globals=globals(), number=1000000))<\/p>\n<h2><strong>\u4f7f\u7528 f-string<\/strong><\/h2>\n<p>print(timeit.timeit(&#39;f&quot;{num}&quot;&#39;, globals=globals(), number=1000000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4ece\u6027\u80fd\u6d4b\u8bd5\u7684\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0cf-string \u901a\u5e38\u662f\u6700\u5feb\u7684\u9009\u62e9\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6709\u5f88\u591a\u5e94\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5c06\u6570\u5b57\u4e0e\u5176\u4ed6\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u751f\u6210\u52a8\u6001\u6587\u4ef6\u540d\u3001\u6784\u5efaURL\u53c2\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u52a8\u6001\u6587\u4ef6\u540d<\/p>\n<p>file_id = 123<\/p>\n<p>file_name = f&quot;file_{file_id}.txt&quot;<\/p>\n<p>print(file_name)  # \u8f93\u51fa\uff1afile_123.txt<\/p>\n<h2><strong>\u6784\u5efaURL\u53c2\u6570<\/strong><\/h2>\n<p>base_url = &quot;https:\/\/example.com\/data&quot;<\/p>\n<p>param = 42<\/p>\n<p>url = f&quot;{base_url}?id={param}&quot;<\/p>\n<p>print(url)  # \u8f93\u51fa\uff1ahttps:\/\/example.com\/data?id=42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0cf-string \u548c format() \u65b9\u6cd5\u663e\u5f97\u5c24\u4e3a\u65b9\u4fbf\u548c\u9ad8\u6548\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5728Python\u7f16\u7a0b\u4e2d\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4e86\u89e3\u5e76\u638c\u63e1\u591a\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u591f\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\uff0c\u8fd8\u80fd\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<strong>\u65e0\u8bba\u662f\u4f7f\u7528 str() \u51fd\u6570\u7684\u7b80\u5355\u76f4\u63a5\uff0c\u8fd8\u662f f-string \u7684\u9ad8\u6548\u6613\u8bfb\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u5c06\u4f7f\u4f60\u7684Python\u7f16\u7a0b\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8f6c\u6362\u6574\u6570\u4e3a\u5b57\u7b26\u4e32\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>str()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>str(123)<\/code>\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32<code>&#39;123&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4efb\u4f55\u6574\u6570\uff0c\u65e0\u8bba\u5176\u5927\u5c0f\u5982\u4f55\u3002<\/p>\n<p><strong>\u4f7f\u7528\u683c\u5f0f\u5316\u65b9\u6cd5\u662f\u5426\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u65b9\u6cd5\u6765\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f7f\u7528f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff0c<code>number = 123<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>f&quot;{number}&quot;<\/code>\u5c06\u4f1a\u5f97\u5230\u5b57\u7b26\u4e32<code>&#39;123&#39;<\/code>\u3002\u6b64\u5916\uff0c<code>format()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u7528\u4e8e\u7c7b\u4f3c\u7684\u76ee\u7684\uff0c\u4f8b\u5982<code>&quot;{}&quot;.format(number)<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u64cd\u4f5c\u7b26\u5c06\u6574\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u3002\u901a\u8fc7\u8fde\u63a5\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u4e0e\u6574\u6570\u53ef\u4ee5\u5b9e\u73b0\u8f6c\u6362\uff0c\u4f8b\u5982\uff0c<code>str_num = &quot;&quot; + str(123)<\/code>\uff0c\u8fd9\u5c06\u5f97\u5230\u5b57\u7b26\u4e32<code>&#39;123&#39;<\/code>\u3002\u4e0d\u8fc7\uff0c\u4e3a\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\uff0c\u5efa\u8bae\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u6216\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570str()\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f7f\u7528f-string\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1080304,"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\/1080298"}],"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=1080298"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080298\/revisions"}],"predecessor-version":[{"id":1080307,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080298\/revisions\/1080307"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080304"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}