{"id":1073005,"date":"2025-01-08T11:21:23","date_gmt":"2025-01-08T03:21:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073005.html"},"modified":"2025-01-08T11:21:25","modified_gmt":"2025-01-08T03:21:25","slug":"python3%e5%a6%82%e4%bd%95%e6%a0%bc%e5%bc%8f%e5%8c%96%e8%be%93%e5%87%ba-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073005.html","title":{"rendered":"python3\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103043\/666470b3-612e-4b83-89f5-ed6ca266fa48.webp\" alt=\"python3\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\" \/><\/p>\n<p><p> <strong>Python3\u683c\u5f0f\u5316\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001\u4f7f\u7528str.format()\u65b9\u6cd5\u3001\u4f7f\u7528f-string\u683c\u5f0f\u5316\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u6a21\u677f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u793a\u4f8b\u3002<\/strong>\u5176\u4e2d\uff0c<strong>f-string\u683c\u5f0f\u5316<\/strong>\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u8fd8\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u662fPython\u4e2d\u6700\u65e9\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7\u4f7f\u7528\u767e\u5206\u53f7(<code>%<\/code>)\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742\u683c\u5f0f\u65f6\u663e\u5f97\u4e0d\u591f\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\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><h4>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>%s<\/code>\u7528\u6765\u8868\u793a\u5b57\u7b26\u4e32\uff0c<code>%d<\/code>\u7528\u6765\u8868\u793a\u6574\u6570\u3002\u53d8\u91cf<code>name<\/code>\u548c<code>age<\/code>\u5206\u522b\u88ab\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u76f8\u5e94\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528str.format()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.format()<\/code>\u65b9\u6cd5\u662fPython 3\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7\u82b1\u62ec\u53f7 <code>{}<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5e76\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u767e\u5206\u53f7\u683c\u5f0f\u5316\u66f4\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\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>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u82b1\u62ec\u53f7 <code>{}<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c<code>name<\/code>\u548c<code>age<\/code>\u53d8\u91cf\u901a\u8fc7<code>format()<\/code>\u65b9\u6cd5\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5728\u82b1\u62ec\u53f7\u4e2d\u6dfb\u52a0\u6570\u5b57\u7d22\u5f15\u6765\u6307\u5b9a\u53d8\u91cf\u7684\u4f4d\u7f6e\uff0c\u4f8b\u5982 <code>{0}<\/code> \u548c <code>{1}<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528f-string\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>f-string\u683c\u5f0f\u5316\u662f\u5728Python 3.6\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0\u5b57\u6bcd<code>f<\/code>\u6216<code>F<\/code>\uff0c\u5e76\u5728\u82b1\u62ec\u53f7 <code>{}<\/code> \u4e2d\u76f4\u63a5\u63d2\u5165\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u8fd8\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Charlie&quot;<\/p>\n<p>age = 28<\/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>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32\u524d\u7684<code>f<\/code>\u8868\u793a\u8fd9\u662f\u4e00\u4e2af-string\uff0c\u82b1\u62ec\u53f7 <code>{}<\/code> \u4e2d\u7684\u53d8\u91cf\u4f1a\u88ab\u76f4\u63a5\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002f-string\u8fd8\u652f\u6301\u5728\u82b1\u62ec\u53f7\u4e2d\u8fdb\u884c\u8868\u8fbe\u5f0f\u8ba1\u7b97\uff0c\u4f8b\u5982 <code>f&quot;{age + 1}&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u6a21\u677f<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6a21\u677f\u662f\u901a\u8fc7<code>string<\/code>\u6a21\u5757\u4e2d\u7684<code>Template<\/code>\u7c7b\u5b9e\u73b0\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528<code>$<\/code>\u7b26\u53f7\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5e76\u901a\u8fc7<code>substitute()<\/code>\u65b9\u6cd5\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\u66f4\u52a0\u5b89\u5168\uff0c\u4f46\u7075\u6d3b\u6027\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\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;David&quot;, age=32))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Template<\/code>\u7c7b\u4f7f\u7528<code>$name<\/code>\u548c<code>$age<\/code>\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c<code>substitute()<\/code>\u65b9\u6cd5\u5c06<code>name<\/code>\u548c<code>age<\/code>\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<code><a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_substitute()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728\u7f3a\u5c11\u67d0\u4e9b\u53d8\u91cf\u65f6\u4e0d\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u5404\u79cd\u683c\u5f0f\u5316\u65b9\u5f0f\u7684\u5bf9\u6bd4<\/h3>\n<\/p>\n<ul>\n<li><strong>\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/strong>\uff1a\u8bed\u6cd5\u7b80\u5355\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742\u683c\u5f0f\u65f6\u4e0d\u76f4\u89c2\u3002<\/li>\n<li><strong>str.format()\u65b9\u6cd5<\/strong>\uff1a\u7075\u6d3b\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u8bed\u6cd5\u7a0d\u663e\u5197\u957f\u3002<\/li>\n<li><strong>f-string\u683c\u5f0f\u5316<\/strong>\uff1a\u8bed\u6cd5\u7b80\u6d01\uff0c\u6027\u80fd\u4f18\u8d8a\uff0c\u63a8\u8350\u4f7f\u7528\u3002<\/li>\n<li><strong>\u5b57\u7b26\u4e32\u6a21\u677f<\/strong>\uff1a\u5b89\u5168\u6027\u9ad8\uff0c\u4f46\u7075\u6d3b\u6027\u4e0d\u8db3\u3002<\/li>\n<\/ul>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python3\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528<strong>f-string\u683c\u5f0f\u5316<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u8fd8\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u5728\u9700\u8981\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5b57\u7b26\u4e32\u6a21\u677f\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u51e0\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u7684\u8f93\u51fa\u9700\u6c42\uff0c\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\u5728Python3\u4e2d\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u6765\u5b9e\u73b0\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0\u5b57\u6bcd\u201cf\u201d\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u53d8\u91cf\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">name = &quot;Alice&quot;\nage = 30\nprint(f&quot;My name is {name} and I am {age} years old.&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7b80\u6d01\u6613\u61c2\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u63d2\u5165\u591a\u4e2a\u53d8\u91cf\u7684\u573a\u666f\u3002<\/p>\n<p><strong>Python3\u4e2d\u6709\u54ea\u4e9b\u5176\u4ed6\u7684\u683c\u5f0f\u5316\u8f93\u51fa\u65b9\u5f0f\uff1f<\/strong><br \/>\u9664\u4e86f-string\uff0cPython3\u8fd8\u652f\u6301\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u548c\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316\u3002\u4f7f\u7528<code>str.format()<\/code>\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">print(&quot;My name is {} and I am {} years old.&quot;.format(name, age))\n<\/code><\/pre>\n<p>\u800c\u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316\u5219\u5982\u4e0b\u6240\u793a\uff1a  <\/p>\n<pre><code class=\"language-python\">print(&quot;My name is %s and I am %d years old.&quot; % (name, age))\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u5f0f\u5728\u5904\u7406\u590d\u6742\u7684\u683c\u5f0f\u65f6\u4e5f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python3\u4e2d\u63a7\u5236\u6570\u5b57\u7684\u683c\u5f0f\u5316\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u8bf4\u660e\u7b26\u6765\u63a7\u5236\u6570\u5b57\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u683c\u5f0f\u5316\u4e00\u4e2a\u6d6e\u70b9\u6570\u5230\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">value = 3.14159\nprint(f&quot;The value is {value:.2f}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4f1a\u8f93\u51fa<code>The value is 3.14<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python3\u683c\u5f0f\u5316\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001\u4f7f\u7528str.format()\u65b9\u6cd5\u3001\u4f7f\u7528f- [&hellip;]","protected":false},"author":3,"featured_media":1073014,"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\/1073005"}],"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=1073005"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073005\/revisions"}],"predecessor-version":[{"id":1073016,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073005\/revisions\/1073016"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073014"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}