{"id":1114989,"date":"2025-01-08T18:02:30","date_gmt":"2025-01-08T10:02:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1114989.html"},"modified":"2025-01-08T18:02:34","modified_gmt":"2025-01-08T10:02:34","slug":"python%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e8%be%93%e5%87%ba%e6%95%b0%e5%ad%97%e5%92%8c%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114989.html","title":{"rendered":"python\u5982\u4f55\u540c\u65f6\u8f93\u51fa\u6570\u5b57\u548c\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\/25075933\/f5b31beb-992f-48e3-8865-00829ddda2b3.webp\" alt=\"python\u5982\u4f55\u540c\u65f6\u8f93\u51fa\u6570\u5b57\u548c\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u540c\u65f6\u8f93\u51fa\u6570\u5b57\u548c\u5b57\u7b26\u4e32<\/strong>\uff0c\u4f8b\u5982\u4f7f\u7528\u9017\u53f7\u5206\u9694\u3001\u52a0\u53f7\u8fde\u63a5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u4ee5\u53ca<code>format()<\/code>\u65b9\u6cd5\u7b49\u3002<strong>\u5176\u4e2d\uff0c\u4f7f\u7528f-string\u662f\u6700\u7b80\u6d01\u548c\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\u3002\u8ba9\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u9017\u53f7\u5206\u9694<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u9017\u53f7\u5206\u9694\u7684<code>print()<\/code>\u51fd\u6570\u4f1a\u81ea\u52a8\u5728\u8f93\u51fa\u4e2d\u63d2\u5165\u7a7a\u683c\uff0c\u5e76\u5c06\u6240\u6709\u53c2\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 42<\/p>\n<p>text = &quot;The answer is&quot;<\/p>\n<p>print(text, number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u4f46\u6709\u4e00\u4e2a\u9650\u5236\uff0c\u5373\u5b83\u4f1a\u5728\u8f93\u51fa\u7684\u5404\u90e8\u5206\u4e4b\u95f4\u63d2\u5165\u7a7a\u683c\u3002\u5982\u679c\u4e0d\u5e0c\u671b\u6709\u7a7a\u683c\uff0c\u5176\u4ed6\u65b9\u6cd5\u53ef\u80fd\u66f4\u9002\u5408\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u52a0\u53f7\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u52a0\u53f7\u8fde\u63a5\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u524d\u9700\u8981\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e2a\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 42<\/p>\n<p>text = &quot;The answer is&quot;<\/p>\n<p>print(text + &quot; &quot; + str(number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u6027\u66f4\u9ad8\uff0c\u53ef\u4ee5\u5b8c\u5168\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u4f46\u4ee3\u7801\u53ef\u8bfb\u6027\u53ef\u80fd\u7a0d\u5dee\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09<\/h3>\n<\/p>\n<p><p>\u4ecePython 3.6\u5f00\u59cb\uff0cf-string\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u65b9\u5f0f\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u5b83\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a<code>f<\/code>\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u5185\u90e8\u4f7f\u7528\u5927\u62ec\u53f7<code>{}<\/code>\u5305\u88f9\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 42<\/p>\n<p>text = &quot;The answer is&quot;<\/p>\n<p>print(f&quot;{text} {number}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-string\u4e0d\u4ec5\u7b80\u5355\u6613\u8bfb\uff0c\u8fd8\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u683c\u5f0f\u5316\u9009\u9879\uff0c\u662f\u63a8\u8350\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python 2.7\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c<code>format()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 42<\/p>\n<p>text = &quot;The answer is&quot;<\/p>\n<p>print(&quot;{} {}&quot;.format(text, number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>format()<\/code>\u65b9\u6cd5\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u76f8\u5bf9\u4e8ef-string\u7a0d\u663e\u5197\u957f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u7684printf\u8bed\u6cd5\uff0c\u4f46\u4e0d\u5982f-string\u548c<code>format()<\/code>\u65b9\u6cd5\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 42<\/p>\n<p>text = &quot;The answer is&quot;<\/p>\n<p>print(&quot;%s %d&quot; % (text, number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u5df2\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u4f46\u5728\u5904\u7406\u65e7\u4ee3\u7801\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u62fc\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\uff0c<code>join()<\/code>\u65b9\u6cd5\u4e5f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 42<\/p>\n<p>text = &quot;The answer is&quot;<\/p>\n<p>print(&quot; &quot;.join([text, str(number)]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>join()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u573a\u666f\uff0c\u4f46\u9700\u8981\u624b\u52a8\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7efc\u5408\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p><strong>\u9017\u53f7\u5206\u9694<\/strong>\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u4f1a\u5728\u8f93\u51fa\u4e2d\u63d2\u5165\u7a7a\u683c\uff0c\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u573a\u666f\uff1b<strong>\u52a0\u53f7\u8fde\u63a5<\/strong>\u65b9\u6cd5\u7075\u6d3b\uff0c\u4f46\u9700\u8981\u624b\u52a8\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff1b<strong>f-string<\/strong>\u65b9\u6cd5\u6700\u7b80\u6d01\u6613\u8bfb\uff0c\u63a8\u8350\u4f7f\u7528\uff1b<strong>format()\u65b9\u6cd5<\/strong>\u529f\u80fd\u5f3a\u5927\u4f46\u7a0d\u663e\u5197\u957f\uff1b<strong>\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/strong>\u65b9\u6cd5\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\uff1b<strong>join()\u65b9\u6cd5<\/strong>\u9002\u7528\u4e8e\u62fc\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\u6765\u540c\u65f6\u8f93\u51fa\u6570\u5b57\u548c\u5b57\u7b26\u4e32\u975e\u5e38\u91cd\u8981\u3002<strong>f-string\u662f\u6700\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u7b80\u6d01\u76f4\u89c2\uff0c\u8fd8\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u683c\u5f0f\u5316\u9009\u9879\u3002\u4e86\u89e3\u548c\u638c\u63e1\u591a\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5730\u540c\u65f6\u8f93\u51fa\u6570\u5b57\u548c\u5b57\u7b26\u4e32\uff0c\u5e76\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u5f0f\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u8f93\u51fa\u8fd8\u662f\u590d\u6742\u7684\u683c\u5f0f\u5316\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u6765\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6570\u5b57\u4e0e\u5b57\u7b26\u4e32\u7684\u6df7\u5408\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u6765\u8f93\u51fa\u6570\u5b57\u548c\u5b57\u7b26\u4e32\u7684\u6df7\u5408\u5185\u5bb9\u3002\u901a\u8fc7\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6216\u8005\u76f4\u63a5\u5728<code>print()<\/code>\u4e2d\u4f7f\u7528\u9017\u53f7\u5206\u9694\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<code>print(&quot;The number is&quot;, 5)<\/code>\u5c06\u8f93\u51fa<code>The number is 5<\/code>\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u4ee5\u5305\u542b\u6570\u5b57\u548c\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u6709\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>f-string<\/code>\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u662f\u5176\u4e2d\u4e00\u79cd\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5199\u6210<code>number = 5<\/code>\u548c<code>print(f&quot;The number is {number}&quot;)<\/code>\uff0c\u8fd9\u6837\u4e0d\u4ec5\u4ee3\u7801\u66f4\u6e05\u6670\uff0c\u4e5f\u6613\u4e8e\u9605\u8bfb\u3002\u5176\u4ed6\u65b9\u6cd5\u8fd8\u5305\u62ec<code>str.format()<\/code>\u548c\u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316\u3002<\/p>\n<p><strong>\u5728\u8f93\u51fa\u65f6\uff0c\u5982\u4f55\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u9009\u9879\u6765\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>:.2f<\/code>\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u683c\u5f0f\u5316\u4e3a\u4e24\u4f4d\u5c0f\u6570\uff0c\u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a<code>value = 3.14159<\/code>\u548c<code>print(f&quot;Value: {value:.2f}&quot;)<\/code>\uff0c\u8f93\u51fa\u5c06\u4e3a<code>Value: 3.14<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6570\u5b57\u4ee5\u60a8\u5e0c\u671b\u7684\u65b9\u5f0f\u5448\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u540c\u65f6\u8f93\u51fa\u6570\u5b57\u548c\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\u4f7f\u7528\u9017\u53f7\u5206\u9694\u3001\u52a0\u53f7\u8fde\u63a5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-stri [&hellip;]","protected":false},"author":3,"featured_media":1114994,"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\/1114989"}],"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=1114989"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114989\/revisions"}],"predecessor-version":[{"id":1114995,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114989\/revisions\/1114995"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114994"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}