{"id":1107825,"date":"2025-01-08T16:53:46","date_gmt":"2025-01-08T08:53:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107825.html"},"modified":"2025-01-08T16:53:48","modified_gmt":"2025-01-08T08:53:48","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e5%bc%95%e7%94%a8%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1107825.html","title":{"rendered":"python\u5982\u4f55\u5728\u5b57\u7b26\u4e32\u4e2d\u5f15\u7528\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071711\/ef0596f7-f9e2-435c-b008-fbd9aa9bcb58.webp\" alt=\"python\u5982\u4f55\u5728\u5b57\u7b26\u4e32\u4e2d\u5f15\u7528\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5f15\u7528\u53d8\u91cf\u5230\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-strings\uff09\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001\u548cstr.format()\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf<\/strong>\u3002\u5176\u4e2d\uff0c<strong>f-strings<\/strong> \u662fPython 3.6\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\u63a8\u8350\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u4e14\u6613\u8bfb\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff09<\/h3>\n<\/p>\n<p><p>f-strings\u662f\u4e00\u79cd\u5728Python 3.6\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\u5f15\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u6d01\u548c\u76f4\u89c2\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u9762\u52a0\u4e0a\u5b57\u6bcd<code>f<\/code>\u6216<code>F<\/code>\uff0c\u7136\u540e\u5728\u5b57\u7b26\u4e32\u5185\u90e8\u4f7f\u7528\u5927\u62ec\u53f7<code>{}<\/code>\u5305\u88f9\u53d8\u91cf\u540d\u6216\u8868\u8fbe\u5f0f\uff0c\u5373\u53ef\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>message = f&quot;Hello, my name is {name} and I am {age} years old.&quot;<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>name<\/code>\u548c<code>age<\/code>\u53d8\u91cf\u88ab\u76f4\u63a5\u63d2\u5165\u5230\u4e86\u5b57\u7b26\u4e32\u4e2d\uff0c\u5f97\u5230\u7684\u8f93\u51fa\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, my name is Alice and I am 30 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>f-strings<\/strong>\u4e0d\u4ec5\u53ef\u4ee5\u63d2\u5165\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u63d2\u5165\u8868\u8fbe\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 10<\/p>\n<p>message = f&quot;The square of {number} is {number  2}.&quot;<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>The square of 10 is 100.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u65e9\u671f\u7684Python\u7248\u672c\u4e2d\uff0c\u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u63d2\u5165\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528<code>%<\/code>\u7b26\u53f7\u548c\u683c\u5f0f\u8bf4\u660e\u7b26\u6765\u8868\u793a\u9700\u8981\u63d2\u5165\u7684\u53d8\u91cf\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Bob&quot;<\/p>\n<p>age = 25<\/p>\n<p>message = &quot;Hello, my name is %s and I am %d years old.&quot; % (name, age)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>%s<\/code>\u548c<code>%d<\/code>\u5206\u522b\u662f\u5b57\u7b26\u4e32\u548c\u6574\u6570\u7684\u683c\u5f0f\u8bf4\u660e\u7b26\uff0c\u5b83\u4eec\u5c06\u88ab<code>name<\/code>\u548c<code>age<\/code>\u53d8\u91cf\u7684\u503c\u66ff\u6362\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, my name is Bob and I am 25 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u767e\u5206\u53f7\u683c\u5f0f\u5316\u65b9\u6cd5\u6bd4\u8f83\u8001\u65e7\uff0c\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u4ecd\u7136\u4f1a\u7528\u5230\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001str.format()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u662fPython 2.7\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u8c03\u7528\u5b57\u7b26\u4e32\u7684<code>format()<\/code>\u65b9\u6cd5\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u5927\u62ec\u53f7<code>{}<\/code>\u6765\u63d2\u5165\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Charlie&quot;<\/p>\n<p>age = 35<\/p>\n<p>message = &quot;Hello, my name is {} and I am {} years old.&quot;.format(name, age)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>format()<\/code>\u65b9\u6cd5\u5c06<code>name<\/code>\u548c<code>age<\/code>\u53d8\u91cf\u7684\u503c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u62ec\u53f7\u4f4d\u7f6e\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, my name is Charlie and I am 35 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c<code>str.format()<\/code>\u65b9\u6cd5\u8fd8\u652f\u6301\u5e26\u6709\u7d22\u5f15\u548c\u547d\u540d\u7684\u5360\u4f4d\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;David&quot;<\/p>\n<p>age = 40<\/p>\n<p>message = &quot;Hello, my name is {0} and I am {1} years old.&quot;.format(name, age)<\/p>\n<p>print(message)<\/p>\n<p>message = &quot;Hello, my name is {name} and I am {age} years old.&quot;.format(name=name, age=age)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e24\u79cd\u65b9\u5f0f\u7684\u8f93\u51fa\u7ed3\u679c\u5206\u522b\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, my name is David and I am 40 years old.<\/p>\n<p>Hello, my name is David and I am 40 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u677f\u5b57\u7b26\u4e32\uff08Template Strings\uff09<\/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\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66f4\u590d\u6742\u5b57\u7b26\u4e32\u6a21\u677f\u7684\u60c5\u51b5\u3002\u6a21\u677f\u5b57\u7b26\u4e32\u4f7f\u7528<code>$<\/code>\u7b26\u53f7\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5e76\u901a\u8fc7<code>Template<\/code>\u7c7b\u7684<code>substitute()<\/code>\u65b9\u6cd5\u6765\u63d2\u5165\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>name = &quot;Eve&quot;<\/p>\n<p>age = 28<\/p>\n<p>template = Template(&quot;Hello, my name is $name and I am $age years old.&quot;)<\/p>\n<p>message = template.substitute(name=name, age=age)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Template<\/code>\u7c7b\u7684<code>substitute()<\/code>\u65b9\u6cd5\u5c06<code>name<\/code>\u548c<code>age<\/code>\u53d8\u91cf\u7684\u503c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u5360\u4f4d\u7b26\u4f4d\u7f6e\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, my name is Eve and I am 28 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5c0f\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5f15\u7528\u53d8\u91cf\u5230\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<strong>f-strings\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001str.format()\u65b9\u6cd5\u3001\u6a21\u677f\u5b57\u7b26\u4e32<\/strong>\u3002\u5176\u4e2d\uff0c<strong>f-strings<\/strong>\u662fPython 3.6\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\u63a8\u8350\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u4e14\u6613\u8bfb\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u5f0f\u5373\u53ef\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u66f4\u7075\u6d3b\u5730\u5904\u7406\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u6218\u6848\u4f8b<\/h3>\n<\/p>\n<p><h4>\u6848\u4f8b\u4e00\uff1a\u52a8\u6001\u751f\u6210\u6587\u4ef6\u540d<\/h4>\n<\/p>\n<p><p>\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u9700\u8981\u52a8\u6001\u751f\u6210\u6587\u4ef6\u540d\uff0c\u4f8b\u5982\u6839\u636e\u65e5\u671f\u65f6\u95f4\u751f\u6210\u65e5\u5fd7\u6587\u4ef6\u540d\u3002\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u5165\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>current_time = datetime.now().strftime(&quot;%Y%m%d_%H%M%S&quot;)<\/p>\n<p>file_name = f&quot;log_{current_time}.txt&quot;<\/p>\n<p>print(file_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u65f6\u95f4\uff0c\u5e76\u901a\u8fc7<code>strftime()<\/code>\u65b9\u6cd5\u5c06\u5176\u683c\u5f0f\u5316\u4e3a<code>YYYYMMDD_HHMMSS<\/code>\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<strong>f-strings<\/strong>\u5c06\u5176\u63d2\u5165\u5230\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\u4e2d\uff0c\u6700\u7ec8\u751f\u6210\u7684\u6587\u4ef6\u540d\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n<\/p>\n<p><pre><code>log_20230401_153045.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6848\u4f8b\u4e8c\uff1a\u751f\u6210SQL\u67e5\u8be2\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u52a8\u6001\u751f\u6210SQL\u67e5\u8be2\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u6761\u4ef6\u751f\u6210\u76f8\u5e94\u7684\u67e5\u8be2\u8bed\u53e5\u3002\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u5165\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6784\u5efaSQL\u67e5\u8be2\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table = &quot;users&quot;<\/p>\n<p>columns = [&quot;id&quot;, &quot;name&quot;, &quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;]<\/p>\n<p>conditions = &quot;age &gt; 30&quot;<\/p>\n<p>query = f&quot;SELECT {&#39;, &#39;.join(columns)} FROM {table} WHERE {conditions};&quot;<\/p>\n<p>print(query)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7<strong>f-strings<\/strong>\u5c06\u8868\u540d\u3001\u5217\u540d\u548c\u6761\u4ef6\u63d2\u5165\u5230SQL\u67e5\u8be2\u5b57\u7b26\u4e32\u4e2d\uff0c\u6700\u7ec8\u751f\u6210\u7684\u67e5\u8be2\u8bed\u53e5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>SELECT id, name, email FROM users WHERE age &gt; 30;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6848\u4f8b\u4e09\uff1a\u751f\u6210HTML\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u52a8\u6001\u751f\u6210HTML\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u751f\u6210\u4e00\u4e2aHTML\u8868\u683c\u3002\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u5165\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6784\u5efaHTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 25},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35}<\/p>\n<p>]<\/p>\n<p>rows = &quot;&quot;<\/p>\n<p>for item in data:<\/p>\n<p>    rows += f&quot;&lt;tr&gt;&lt;td&gt;{item[&#39;name&#39;]}&lt;\/td&gt;&lt;td&gt;{item[&#39;age&#39;]}&lt;\/td&gt;&lt;\/tr&gt;&quot;<\/p>\n<p>html = f&quot;&quot;&quot;<\/p>\n<p>&lt;table border=&quot;1&quot;&gt;<\/p>\n<p>    &lt;tr&gt;<\/p>\n<p>        &lt;th&gt;Name&lt;\/th&gt;<\/p>\n<p>        &lt;th&gt;Age&lt;\/th&gt;<\/p>\n<p>    &lt;\/tr&gt;<\/p>\n<p>    {rows}<\/p>\n<p>&lt;\/table&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7<strong>f-strings<\/strong>\u5c06\u6570\u636e\u63d2\u5165\u5230HTML\u8868\u683c\u5b57\u7b26\u4e32\u4e2d\uff0c\u6700\u7ec8\u751f\u6210\u7684HTML\u5185\u5bb9\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;table border=&quot;1&quot;&gt;<\/p>\n<p>    &lt;tr&gt;<\/p>\n<p>        &lt;th&gt;Name&lt;\/th&gt;<\/p>\n<p>        &lt;th&gt;Age&lt;\/th&gt;<\/p>\n<p>    &lt;\/tr&gt;<\/p>\n<p>    &lt;tr&gt;&lt;td&gt;Alice&lt;\/td&gt;&lt;td&gt;30&lt;\/td&gt;&lt;\/tr&gt;&lt;tr&gt;&lt;td&gt;Bob&lt;\/td&gt;&lt;td&gt;25&lt;\/td&gt;&lt;\/tr&gt;&lt;tr&gt;&lt;td&gt;Charlie&lt;\/td&gt;&lt;td&gt;35&lt;\/td&gt;&lt;\/tr&gt;<\/p>\n<p>&lt;\/table&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u65f6\uff0c\u7279\u522b\u662f\u5728\u751f\u6210SQL\u67e5\u8be2\u8bed\u53e5\u65f6\uff0c\u8981\u7279\u522b\u6ce8\u610f\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\u3002\u5e94\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u6216ORM\u5de5\u5177\u6765\u751f\u6210\u5b89\u5168\u7684SQL\u67e5\u8be2\u8bed\u53e5\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5728\u9700\u8981\u9ad8\u6027\u80fd\u7684\u573a\u666f\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5b57\u7b26\u4e32\u63d2\u5165\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<strong>f-strings<\/strong>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u597d\uff0c\u4f46\u5728\u5927\u91cf\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\uff0c<code>join()<\/code>\u65b9\u6cd5\u53ef\u80fd\u66f4\u9ad8\u6548\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u5728\u9009\u62e9\u5b57\u7b26\u4e32\u63d2\u5165\u65b9\u6cd5\u65f6\uff0c\u8981\u8003\u8651Python\u7248\u672c\u7684\u517c\u5bb9\u6027\u3002\u4f8b\u5982\uff0c<strong>f-strings<\/strong>\u4ec5\u9002\u7528\u4e8ePython 3.6\u53ca\u4ee5\u540e\u7248\u672c\u3002<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86Python\u4e2d\u5f15\u7528\u53d8\u91cf\u5230\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\uff0c\u5305\u62ec<strong>f-strings\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001str.format()\u65b9\u6cd5<\/strong>\u548c<strong>\u6a21\u677f\u5b57\u7b26\u4e32<\/strong>\u3002\u901a\u8fc7\u5bf9\u6bd4\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u4ee5\u53ca\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u5b57\u7b26\u4e32\u63d2\u5165\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\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\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-strings\uff09\u3001<code>str.format()<\/code>\u65b9\u6cd5\u4ee5\u53ca\u767e\u5206\u53f7\u683c\u5f0f\u5316\uff08%\uff09\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-strings\u7684\u65b9\u5f0f\u662f<code>name = &quot;Alice&quot;; greeting = f&quot;Hello, {name}!&quot;<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\uff0c\u786e\u4fdd\u6240\u6709\u8981\u62fc\u63a5\u7684\u5185\u5bb9\u90fd\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u5982\u679c\u53d8\u91cf\u662f\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>age = 25; message = &quot;I am &quot; + str(age) + &quot; years old.&quot;<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<p><strong>Python\u4e2d\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u6a21\u677f\u5b57\u7b26\u4e32\uff08Template Strings\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u5b89\u5168\u548c\u53ef\u8bfb\u7684\u65b9\u5f0f\u6765\u63d2\u5165\u53d8\u91cf\u3002\u4f7f\u7528<code>string<\/code>\u6a21\u5757\u4e2d\u7684<code>Template<\/code>\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>$<\/code>\u7b26\u53f7\u6765\u5f15\u7528\u53d8\u91cf\uff0c\u4f8b\u5982\uff1a<code>from string import Template; t = Template(&quot;Hello, $name&quot;); result = t.substitute(name=&quot;Bob&quot;)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u573a\u666f\uff0c\u80fd\u6709\u6548\u907f\u514d\u6ce8\u5165\u653b\u51fb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5f15\u7528\u53d8\u91cf\u5230\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-strings\uff09\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001\u548cst [&hellip;]","protected":false},"author":3,"featured_media":1107830,"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\/1107825"}],"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=1107825"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107825\/revisions"}],"predecessor-version":[{"id":1107832,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107825\/revisions\/1107832"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1107830"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1107825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1107825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1107825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}