{"id":1054899,"date":"2024-12-31T14:46:08","date_gmt":"2024-12-31T06:46:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054899.html"},"modified":"2024-12-31T14:46:12","modified_gmt":"2024-12-31T06:46:12","slug":"python%e5%a6%82%e4%bd%95%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1054899.html","title":{"rendered":"python\u5982\u4f55\u683c\u5f0f\u5316\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1809aee8-8c4d-4c7b-90a8-14c88efe7731.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u3001\u4f7f\u7528str.format()\u65b9\u6cd5\u3001\u4f7f\u7528f-strings (formatted string literals)\u3001\u4f7f\u7528Template\u7c7b\u3002\u4f7f\u7528f-strings\u662fPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u3001\u76f4\u89c2\u3001\u6027\u80fd\u4f18\u8d8a\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u662f\u4e00\u9879\u5e38\u7528\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u3001\u6613\u8bfb\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u7ef4\u62a4\u6027\u3002\u4ee5\u4e0b\u662f\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09<\/h3>\n<\/p>\n<p><p>\u767e\u5206\u53f7\uff08%\uff09\u662fPython\u4e2d\u6700\u65e9\u671f\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u73b0\u5728\u4e0d\u592a\u63a8\u8350\u4f7f\u7528\uff0c\u4f46\u5728\u7ef4\u62a4\u65e7\u4ee3\u7801\u65f6\u4ecd\u7136\u4f1a\u9047\u5230\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;My name is %s and I am %d years old.&quot; % (name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c%s\u7528\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c%d\u7528\u6765\u683c\u5f0f\u5316\u6574\u6570\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u66f4\u591a\u7684\u683c\u5f0f\u5316\u7b26\u53f7\u6765\u5904\u7406\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<ul>\n<li>%s\uff1a\u5b57\u7b26\u4e32<\/li>\n<li>%d\uff1a\u6574\u6570<\/li>\n<li>%f\uff1a\u6d6e\u70b9\u6570<\/li>\n<li>%x\uff1a\u5341\u516d\u8fdb\u5236\u6574\u6570<\/li>\n<\/ul>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u683c\u5f0f\u5316\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u53ef\u8bfb\u6027\u5dee\uff0c\u4e0d\u63a8\u8350\u5728\u590d\u6742\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u573a\u666f\u4e2d\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528str.format()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u63d0\u4f9b\u4e86\u6bd4\u767e\u5206\u53f7\uff08%\uff09\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u548c\u5d4c\u5957\u5b57\u6bb5\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Bob&quot;<\/p>\n<p>age = 25<\/p>\n<p>print(&quot;My name is {} and I am {} years old.&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;My name is {0} and I am {1} years old.&quot;.format(name, age))<\/p>\n<p>print(&quot;My name is {name} and I am {age} years old.&quot;.format(name=&quot;Charlie&quot;, age=28))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5d4c\u5957\u5b57\u6bb5\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&#39;name&#39;: &#39;David&#39;, &#39;age&#39;: 35}<\/p>\n<p>print(&quot;My name is {0[name]} and I am {0[age]} years old.&quot;.format(person))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u7075\u6d3b\u6027\u5f3a\uff0c\u652f\u6301\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u548c\u5d4c\u5957\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u663e\u5f97\u8f83\u4e3a\u5197\u957f\uff0c\u4e0d\u5982f-strings\u7b80\u6d01\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528f-strings (formatted string literals)<\/h3>\n<\/p>\n<p><p>f-strings\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u3002\u5b83\u4f7f\u7528\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Eve&quot;<\/p>\n<p>age = 22<\/p>\n<p>print(f&quot;My name is {name} and I am {age} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8868\u8fbe\u5f0f\u5d4c\u5165\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">print(f&quot;Next year, I will be {age + 1} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u8bed\u6cd5\u7b80\u6d01\u76f4\u89c2\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u9ad8\u3002<\/li>\n<li>\u6027\u80fd\u4f18\u8d8a\uff0c\u6bd4str.format()\u548c\u767e\u5206\u53f7\uff08%\uff09\u65b9\u5f0f\u66f4\u5feb\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u4ec5\u9002\u7528\u4e8ePython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528Template\u7c7b<\/h3>\n<\/p>\n<p><p>Template\u7c7b\u662fstring\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u5ea6\u81ea\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u4efb\u52a1\u3002\u5b83\u4f7f\u7528\u7b80\u5355\u7684\u5360\u4f4d\u7b26\uff08$identifier\uff09\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>template = Template(&quot;My name is $name and I am $age years old.&quot;)<\/p>\n<p>print(template.substitute(name=&quot;Frank&quot;, age=40))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u5ea6\u81ea\u5b9a\u4e49\u7684\u573a\u666f\u3002<\/li>\n<li>\u63d0\u4f9b\u7b80\u5355\u7684\u5360\u4f4d\u7b26\u66ff\u6362\u673a\u5236\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u573a\u666f\u76f8\u5bf9\u8f83\u5c11\uff0c\u4e0d\u5982f-strings\u76f4\u89c2\u3002<\/li>\n<\/ul>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u4f7f\u7528f-strings\u662f\u63a8\u8350\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u8bed\u6cd5\u7b80\u6d01\u3001\u76f4\u89c2\uff0c\u6027\u80fd\u4f18\u8d8a\u3002\u5bf9\u4e8e\u7ef4\u62a4\u65e7\u4ee3\u7801\u6216\u5904\u7406\u7279\u5b9a\u683c\u5f0f\u5316\u4efb\u52a1\u65f6\uff0cstr.format()\u65b9\u6cd5\u548c\u767e\u5206\u53f7\uff08%\uff09\u65b9\u5f0f\u4ecd\u7136\u6709\u5176\u5e94\u7528\u573a\u666f\u3002Template\u7c7b\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u5ea6\u81ea\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u4efb\u52a1\u3002\u9009\u62e9\u5408\u9002\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5360\u4f4d\u7b26\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u5360\u4f4d\u7b26\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u60a8\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u4f8b\u5982\uff0c<code>&quot;Hello, %s&quot; % name<\/code> \u53ef\u4ee5\u5c06\u53d8\u91cf <code>name<\/code> \u7684\u503c\u66ff\u6362\u5230\u5b57\u7b26\u4e32\u4e2d\u7684 <code>%s<\/code> \u4f4d\u7f6e\u3002\u6b64\u5916\uff0c\u4f7f\u7528 <code>%d<\/code> \u53ef\u4ee5\u63d2\u5165\u6574\u6570\uff0c<code>%f<\/code> \u53ef\u4ee5\u63d2\u5165\u6d6e\u70b9\u6570\u3002\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u4ecd\u7136\u53ef\u7528\uff0c\u4f46\u63a8\u8350\u4f7f\u7528\u66f4\u65b0\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3002<\/p>\n<p><strong>Python\u4e2d\u7684f-string\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\uff1f<\/strong><br \/>f-string\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u4f7f\u7528f-string\uff0c\u53ea\u9700\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd\u201cf\u201d\uff0c\u5e76\u5728\u5927\u62ec\u53f7\u4e2d\u76f4\u63a5\u5f15\u7528\u53d8\u91cf\uff0c\u4f8b\u5982\uff1a<code>f&quot;Hello, {name}&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u66f4\u6613\u8bfb\uff0c\u8fd8\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u9891\u7e41\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528str.format()\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff1f<\/strong><br \/><code>str.format()<\/code>\u65b9\u6cd5\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f4d\u7f6e\u548c\u5173\u952e\u5b57\u53c2\u6570\u6765\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c<code>&quot;Hello, {}&quot;.format(name)<\/code> \u5c06 <code>name<\/code> \u7684\u503c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6216\u5173\u952e\u5b57\u6765\u66f4\u7cbe\u786e\u5730\u63a7\u5236\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a<code>&quot;Hello, {0}&quot;.format(name)<\/code> \u6216 <code>&quot;{greeting}, {name}&quot;.format(greeting=&quot;Hello&quot;, name=&quot;Alice&quot;)<\/code>\uff0c\u8fd9\u6837\u7684\u7075\u6d3b\u6027\u4f7f\u5f97\u5b83\u5728\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u3001\u4f7f\u7528str.format()\u65b9\u6cd5\u3001\u4f7f\u7528f-strings [&hellip;]","protected":false},"author":3,"featured_media":1054910,"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\/1054899"}],"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=1054899"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054899\/revisions"}],"predecessor-version":[{"id":1054912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054899\/revisions\/1054912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1054910"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1054899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1054899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1054899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}