{"id":1178725,"date":"2025-01-15T18:14:21","date_gmt":"2025-01-15T10:14:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178725.html"},"modified":"2025-01-15T18:14:24","modified_gmt":"2025-01-15T10:14:24","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%9a%84%e5%8d%a0%e4%bd%8d%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178725.html","title":{"rendered":"\u5982\u4f55\u7528python\u7684\u5360\u4f4d\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113328\/f121a05e-9e5b-4602-8d4c-8dcdb69d84b2.webp\" alt=\"\u5982\u4f55\u7528python\u7684\u5360\u4f4d\u7b26\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7684\u5360\u4f4d\u7b26\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-strings\u548c.format()\u65b9\u6cd5\u3002\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-strings\u548c.format()\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0cf-strings \u662f\u4e00\u79cd\u66f4\u7b80\u6d01\u3001\u76f4\u89c2\u7684\u65b9\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u7ecf\u5178\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\u3002\u4f7f\u7528 <code>%<\/code> \u64cd\u4f5c\u7b26\u53ef\u4ee5\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u5360\u4f4d\u7b26\u4f4d\u7f6e\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 = 25<\/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\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>%s<\/code> \u548c <code>%d<\/code> \u662f\u5360\u4f4d\u7b26\uff0c\u5206\u522b\u7528\u4e8e\u5b57\u7b26\u4e32\u548c\u6574\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728\u8f83\u65e9\u7684Python\u7248\u672c\u4e2d\u4f7f\u7528\u8f83\u591a\uff0c\u4f46\u9010\u6e10\u88ab\u66f4\u73b0\u4ee3\u7684\u65b9\u6cd5\u6240\u53d6\u4ee3\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001.format()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>.format()\u65b9\u6cd5\u662fPython 2.7\u548cPython 3\u7684\u66f4\u73b0\u4ee3\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u5b83\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u4f4d\u7f6e\u6216\u8005\u540d\u79f0\u6765\u63d2\u5165\u53d8\u91cf\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 = 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\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>.format()\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u4f4d\u7f6e\u6216\u8005\u540d\u79f0\u6765\u63d2\u5165\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u6307\u5b9a\u53d8\u91cf\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;My name is {0} and I am {1} years old. {0} is learning Python.&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u6765\u63d2\u5165\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;My name is {name} and I am {age} years old.&quot;.format(name=&quot;Alice&quot;, age=25))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/h3>\n<\/p>\n<p><p>f-strings \u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u7b80\u6d01\u3001\u66f4\u76f4\u89c2\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u5b83\u4f7f\u7528 <code>f<\/code> \u524d\u7f00\u548c\u82b1\u62ec\u53f7 <code>{}<\/code> \u6765\u5d4c\u5165\u8868\u8fbe\u5f0f\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 = 25<\/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\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>f-strings \u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\uff0c\u800c\u4e0d\u9700\u8981\u989d\u5916\u7684\u65b9\u6cd5\u8c03\u7528\u3002\u5b83\u4e0d\u4ec5\u652f\u6301\u53d8\u91cf\u63d2\u503c\uff0c\u8fd8\u652f\u6301\u4efb\u610f\u7684Python\u8868\u8fbe\u5f0f\uff1a<\/p>\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><p>f-strings \u4e5f\u53ef\u4ee5\u4e0e\u5b57\u5178\u548c\u5217\u8868\u7b49\u6570\u636e\u7ed3\u6784\u4e00\u8d77\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}<\/p>\n<p>print(f&quot;My name is {person[&#39;name&#39;]} and I am {person[&#39;age&#39;]} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u677f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6a21\u677f\u5b57\u7b26\u4e32\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684 <code>string<\/code> \u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528 <code>$<\/code> \u7b26\u53f7\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7 <code>Template<\/code> \u7c7b\u6765\u521b\u5efa\u6a21\u677f\u5b57\u7b26\u4e32\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>t = Template(&quot;My name is $name and I am $age years old.&quot;)<\/p>\n<p>print(t.substitute(name=&quot;Alice&quot;, age=25))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u6a21\u677f\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u4e0d\u53d7\u4fe1\u4efb\u7684\u8f93\u5165\u65f6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u907f\u514d\u4e0d\u53d7\u4fe1\u4efb\u7684\u8f93\u5165\u5bfc\u81f4\u7684\u4ee3\u7801\u6ce8\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    t = Template(&quot;My name is $name and I am $age years old.&quot;)<\/p>\n<p>    print(t.substitute(name=&quot;Alice&quot;, age=25))<\/p>\n<p>except KeyError as e:<\/p>\n<p>    print(f&quot;Missing key: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5360\u4f4d\u7b26\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u4f60\u7684Python\u7248\u672c\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u63a8\u8350\u4f7f\u7528f-strings\uff0c\u56e0\u4e3a\u5b83\u66f4\u7b80\u6d01\u76f4\u89c2\u3002\u5982\u679c\u4f60\u9700\u8981\u517c\u5bb9Python 2.7\uff0c\u53ef\u4ee5\u4f7f\u7528.format()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4fdd\u6301\u4ee3\u7801\u53ef\u8bfb\u6027<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5360\u4f4d\u7b26\u65f6\uff0c\u5c3d\u91cf\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u6027\u3002\u907f\u514d\u4f7f\u7528\u8fc7\u591a\u7684\u5d4c\u5957\u548c\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e0d\u53d7\u4fe1\u4efb\u7684\u8f93\u5165\u65f6\uff0c\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\u53ef\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\uff0c\u5e76\u4e14\u5728\u4f7f\u7528\u5360\u4f4d\u7b26\u65f6\uff0c\u6ce8\u610f\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982KeyError\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u907f\u514d\u91cd\u590d\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u91cd\u590d\u4f7f\u7528\u76f8\u540c\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5b57\u7b26\u4e32\u6a21\u677f\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff0c\u5e76\u5728\u9700\u8981\u7684\u5730\u65b9\u91cd\u590d\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">GREETING_TEMPLATE = &quot;My name is {name} and I am {age} years old.&quot;<\/p>\n<p>def greet(name, age):<\/p>\n<p>    return GREETING_TEMPLATE.format(name=name, age=age)<\/p>\n<p>print(greet(&quot;Alice&quot;, 25))<\/p>\n<p>print(greet(&quot;Bob&quot;, 30))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u65e5\u5fd7\u8bb0\u5f55\u4ee3\u7801\u65f6\uff0c\u4f7f\u7528\u5360\u4f4d\u7b26\u53ef\u4ee5\u63d0\u9ad8\u65e5\u5fd7\u4fe1\u606f\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-strings\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>logging.info(f&quot;My name is {name} and I am {age} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u52a8\u6001\u751f\u6210SQL\u67e5\u8be2<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5e93\u67e5\u8be2\u65f6\uff0c\u4f7f\u7528\u5360\u4f4d\u7b26\u53ef\u4ee5\u52a8\u6001\u751f\u6210SQL\u67e5\u8be2\u8bed\u53e5\u3002\u6ce8\u610f\u5728\u6784\u5efaSQL\u67e5\u8be2\u65f6\uff0c\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u6765\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>cursor.execute(&quot;SELECT * FROM users WHERE name = ?&quot;, (name,))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u751f\u6210HTML\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u5728\u751f\u6210HTML\u5185\u5bb9\u65f6\uff0c\u4f7f\u7528\u5360\u4f4d\u7b26\u53ef\u4ee5\u52a8\u6001\u63d2\u5165\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-strings\u751f\u6210HTML\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>html_content = f&quot;&lt;p&gt;My name is {name} and I am {age} years old.&lt;\/p&gt;&quot;<\/p>\n<p>print(html_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u56fd\u9645\u5316\u548c\u672c\u5730\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u8bed\u8a00\u652f\u6301\u65f6\uff0c\u4f7f\u7528\u5360\u4f4d\u7b26\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d2\u5165\u7ffb\u8bd1\u540e\u7684\u6587\u672c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528gettext\u6a21\u5757\u8fdb\u884c\u56fd\u9645\u5316\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gettext<\/p>\n<p>_ = gettext.gettext<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>print(_(&quot;My name is {name} and I am {age} years old.&quot;).format(name=name, age=age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684\u5360\u4f4d\u7b26\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001.format()\u65b9\u6cd5\u3001f-strings\u548c\u6a21\u677f\u5b57\u7b26\u4e32\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548cPython\u7248\u672c\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528\u5360\u4f4d\u7b26\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u65e5\u5fd7\u8bb0\u5f55\u3001\u52a8\u6001\u751f\u6210SQL\u67e5\u8be2\u3001\u751f\u6210HTML\u5185\u5bb9\u548c\u8fdb\u884c\u56fd\u9645\u5316\u5904\u7406\u3002\u901a\u8fc7\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4ee3\u7801\u7684\u5b89\u5168\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5360\u4f4d\u7b26\u5728Python\u4e2d\u6709\u4ec0\u4e48\u5177\u4f53\u5e94\u7528\uff1f<\/strong><br \/>\u5360\u4f4d\u7b26\u5728Python\u4e2d\u4e3b\u8981\u7528\u4e8e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002\u53ef\u4ee5\u4f7f\u7528<code>%<\/code>\u64cd\u4f5c\u7b26\u3001<code>str.format()<\/code>\u65b9\u6cd5\u6216f-string\uff08Python 3.6\u53ca\u4ee5\u4e0a\uff09\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u5360\u4f4d\u7b26\uff0c\u53ef\u4ee5\u5c06\u53d8\u91cf\u7684\u503c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u4ece\u800c\u4f7f\u5b57\u7b26\u4e32\u66f4\u5177\u52a8\u6001\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-string\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u5982<code>name = &quot;Alice&quot;<\/code>\uff0c<code>greeting = f&quot;Hello, {name}!&quot;<\/code>\uff0c\u7ed3\u679c\u5c06\u662f<code>&quot;Hello, Alice!&quot;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u4e0d\u540c\u7c7b\u578b\u7684\u5360\u4f4d\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u5360\u4f4d\u7b26\u53ef\u4f9b\u9009\u62e9\uff0c\u4e3b\u8981\u5305\u62ec<code>%s<\/code>\uff08\u5b57\u7b26\u4e32\uff09\u3001<code>%d<\/code>\uff08\u6574\u6570\uff09\u3001<code>%f<\/code>\uff08\u6d6e\u70b9\u6570\uff09\u7b49\u3002\u9009\u62e9\u4f55\u79cd\u5360\u4f4d\u7b26\u53d6\u51b3\u4e8e\u4f60\u5e0c\u671b\u63d2\u5165\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>%.2f<\/code>\u6765\u683c\u5f0f\u5316\u4e3a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u5360\u4f4d\u7b26\u975e\u5e38\u9002\u5408\u4e8e\u521b\u5efa\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5360\u4f4d\u7b26\u65f6\u5e38\u89c1\u7684\u9519\u8bef\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u5360\u4f4d\u7b26\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u5360\u4f4d\u7b26\u4e0e\u63d0\u4f9b\u7684\u503c\u7c7b\u578b\u4e0d\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528<code>%d<\/code>\u5360\u4f4d\u7b26\u6765\u683c\u5f0f\u5316\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7a0b\u5e8f\u5c06\u629b\u51faTypeError\u3002\u6b64\u5916\uff0c\u7f3a\u5c11\u5360\u4f4d\u7b26\u6240\u9700\u7684\u53d8\u91cf\u4e5f\u4f1a\u5bfc\u81f4\u9519\u8bef\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u683c\u5f0f\u5316\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u5360\u4f4d\u7b26\u6570\u91cf\u4e0e\u63d0\u4f9b\u7684\u53d8\u91cf\u6570\u91cf\u4e00\u81f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6216f-string\u53ef\u4ee5\u51cf\u5c11\u8fd9\u79cd\u9519\u8bef\uff0c\u56e0\u4e3a\u5b83\u4eec\u5141\u8bb8\u66f4\u7075\u6d3b\u7684\u53d8\u91cf\u5f15\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7684\u5360\u4f4d\u7b26\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-strings\u548c.format()\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1178728,"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\/1178725"}],"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=1178725"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178725\/revisions"}],"predecessor-version":[{"id":1178729,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178725\/revisions\/1178729"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178728"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}