{"id":1047742,"date":"2024-12-31T13:45:04","date_gmt":"2024-12-31T05:45:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1047742.html"},"modified":"2024-12-31T13:45:07","modified_gmt":"2024-12-31T05:45:07","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e4%b8%a4%e4%b8%aa%e5%8d%a0%e4%bd%8d%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1047742.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528\u4e24\u4e2a\u5360\u4f4d\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d629356a-2c1e-4866-9cc9-bada535d6aa7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4f7f\u7528\u4e24\u4e2a\u5360\u4f4d\u7b26\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\u4f7f\u7528\u4e24\u4e2a\u5360\u4f4d\u7b26\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528 <code>%<\/code> \u64cd\u4f5c\u7b26\u3001<code>str.format()<\/code> \u65b9\u6cd5\u548c f-string\u3002\u63a8\u8350\u4f7f\u7528 f-string\uff0c\u56e0\u4e3a\u5b83\u66f4\u7b80\u6d01\u4e14\u6613\u8bfb\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1af-string \u662f Python 3.6 \u5f15\u5165\u7684\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0\u5b57\u6bcd &#39;f&#39; \u6216 &#39;F&#39;\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u6765\u5305\u542b\u8868\u8fbe\u5f0f\u6216\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u4f7f\u7528\u5360\u4f4d\u7b26\uff0c\u8fd8\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528\uff0c\u6781\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8 Python \u4e2d\u4f7f\u7528\u4e24\u4e2a\u5360\u4f4d\u7b26\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec <code>%<\/code> \u64cd\u4f5c\u7b26\u3001<code>str.format()<\/code> \u65b9\u6cd5\u548c f-string\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u8bf4\u660e\u5b83\u4eec\u7684\u7528\u6cd5\u548c\u7279\u70b9\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528 <code>%<\/code> \u64cd\u4f5c\u7b26<\/strong><\/h2>\n<p><p><code>%<\/code> \u64cd\u4f5c\u7b26\u662f Python \u4e2d\u6700\u65e9\u4f7f\u7528\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u4e4b\u4e00\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c06\u683c\u5f0f\u5b57\u7b26\u4e32\u548c\u540e\u9762\u7684\u53d8\u91cf\u901a\u8fc7 <code>%<\/code> \u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: %s, Age: %d&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\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4e2d\u7684\u5360\u4f4d\u7b26\uff0c\u5206\u522b\u8868\u793a\u5b57\u7b26\u4e32\u548c\u6574\u6570\u3002<code>%<\/code> \u64cd\u4f5c\u7b26\u540e\u9762\u8ddf\u4e00\u4e2a\u5143\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u8981\u63d2\u5165\u5230\u5360\u4f4d\u7b26\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4f18\u70b9<\/h2>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u76f4\u89c2\uff0c\u5bb9\u6613\u7406\u89e3\u3002<\/li>\n<li>\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/li>\n<\/ul>\n<p><h2>\u7f3a\u70b9<\/h2>\n<\/p>\n<ul>\n<li>\u53ef\u8bfb\u6027\u8f83\u5dee\uff0c\u5c24\u5176\u662f\u5f53\u5360\u4f4d\u7b26\u8f83\u591a\u65f6\u3002<\/li>\n<li>\u4e0d\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528\u3002<\/li>\n<\/ul>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528 <code>str.format()<\/code> \u65b9\u6cd5<\/strong><\/h2>\n<p><p><code>str.format()<\/code> \u65b9\u6cd5\u662f\u5728 Python 2.7 \u548c 3.0 \u4e2d\u5f15\u5165\u7684\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c06\u683c\u5f0f\u5b57\u7b26\u4e32\u4e2d\u7684\u5360\u4f4d\u7b26\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u5305\u56f4\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u540e\u9762\u8c03\u7528 <code>format()<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: {}, Age: {}&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u82b1\u62ec\u53f7 <code>{}<\/code> \u662f\u5360\u4f4d\u7b26\uff0c<code>format()<\/code> \u65b9\u6cd5\u7684\u53c2\u6570\u4f1a\u6309\u987a\u5e8f\u63d2\u5165\u5230\u5360\u4f4d\u7b26\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4f18\u70b9<\/h2>\n<\/p>\n<ul>\n<li>\u7075\u6d3b\u6027\u5f3a\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u6307\u5b9a\u63d2\u5165\u503c\u3002<\/li>\n<li>\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: {0}, Age: {1}&quot;.format(name, age))<\/p>\n<p>print(&quot;Name: {name}, Age: {age}&quot;.format(name=&#39;Alice&#39;, age=30))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u7f3a\u70b9<\/h2>\n<\/p>\n<ul>\n<li>\u76f8\u5bf9\u4e8e f-string\uff0c\u8bed\u6cd5\u7a0d\u663e\u5197\u957f\u3002<\/li>\n<li>\u9700\u8981\u8bb0\u4f4f\u53c2\u6570\u7684\u4f4d\u7f6e\u6216\u540d\u79f0\u3002<\/li>\n<\/ul>\n<h2><strong>\u4e09\u3001\u4f7f\u7528 f-string<\/strong><\/h2>\n<p><p>f-string \u662f Python 3.6 \u5f15\u5165\u7684\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0\u5b57\u6bcd &#39;f&#39; \u6216 &#39;F&#39;\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u6765\u5305\u542b\u8868\u8fbe\u5f0f\u6216\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u82b1\u62ec\u53f7 <code>{}<\/code> \u4e2d\u76f4\u63a5\u5305\u542b\u53d8\u91cf\u540d\uff0cPython \u4f1a\u81ea\u52a8\u5c06\u53d8\u91cf\u7684\u503c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4f18\u70b9<\/h2>\n<\/p>\n<ul>\n<li>\u8bed\u6cd5\u7b80\u6d01\uff0c\u6613\u8bfb\u6613\u5199\u3002<\/li>\n<li>\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>radius = 5<\/p>\n<p>print(f&quot;The area of the circle is {math.pi * radius  2:.2f}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u82b1\u62ec\u53f7 <code>{}<\/code> \u4e2d\u4e0d\u4ec5\u53ef\u4ee5\u5305\u542b\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u5305\u542b\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528\uff0c\u6781\u5927\u63d0\u5347\u4e86\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u7f3a\u70b9<\/h2>\n<\/p>\n<ul>\n<li>\u4ec5\u652f\u6301 Python 3.6 \u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<li>\u5bf9\u4e8e\u8f83\u65e9\u7248\u672c\u7684 Python \u9700\u8981\u4f7f\u7528\u5176\u4ed6\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<h2><strong>\u56db\u3001\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5bf9\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u4e09\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u4f7f\u7528\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u63a8\u8350\u4f7f\u7528 <code>str.format()<\/code> \u65b9\u6cd5\u548c f-string\u3002\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u548c\u4e2a\u4eba\u504f\u597d\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5982\u679c\u4f60\u9700\u8981\u517c\u5bb9 Python 2 \u548c Python 3\uff0c\u63a8\u8350\u4f7f\u7528 <code>str.format()<\/code> \u65b9\u6cd5\u3002<\/li>\n<li>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f Python 3.6 \u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u63a8\u8350\u4f7f\u7528 f-string\uff0c\u56e0\u4e3a\u5b83\u66f4\u7b80\u6d01\u4e14\u6613\u8bfb\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<h2><strong>\u4f7f\u7528 % \u64cd\u4f5c\u7b26<\/strong><\/h2>\n<p>print(&quot;Name: %s, Age: %d&quot; % (name, age))<\/p>\n<h2><strong>\u4f7f\u7528 str.format() \u65b9\u6cd5<\/strong><\/h2>\n<p>print(&quot;Name: {}, Age: {}&quot;.format(name, age))<\/p>\n<p>print(&quot;Name: {name}, Age: {age}&quot;.format(name=&#39;Alice&#39;, age=30))<\/p>\n<h2><strong>\u4f7f\u7528 f-string<\/strong><\/h2>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5bf9\u6bd4\u548c\u603b\u7ed3\uff0c\u53ef\u4ee5\u770b\u51fa f-string \u662f\u76ee\u524d\u6700\u63a8\u8350\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5c24\u5176\u662f\u5728 Python 3.6 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u3002\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u8fd8\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528\uff0c\u5927\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u751f\u6210\u52a8\u6001\u5185\u5bb9\u548c\u8c03\u8bd5\u4fe1\u606f\u65f6\u3002\u4e0b\u9762\u5217\u51fa\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u4f7f\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<p><h2>1\u3001\u751f\u6210\u52a8\u6001\u5185\u5bb9<\/h2>\n<\/p>\n<p><p>\u5728 Web \u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u751f\u6210\u52a8\u6001\u7684 HTML \u5185\u5bb9\u3002\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d2\u5165\u53d8\u91cf\u503c\uff0c\u751f\u6210\u52a8\u6001\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>html = f&quot;&lt;div&gt;&lt;p&gt;Name: {name}&lt;\/p&gt;&lt;p&gt;Age: {age}&lt;\/p&gt;&lt;\/div&gt;&quot;<\/p>\n<p>print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u7528\u6237\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u751f\u6210\u63d0\u793a\u4fe1\u606f\u548c\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = input(&quot;Enter your name: &quot;)<\/p>\n<p>age = input(&quot;Enter your age: &quot;)<\/p>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u8c03\u8bd5\u4fe1\u606f<\/h2>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u8f93\u51fa\u53d8\u91cf\u503c\u548c\u72b6\u6001\u4fe1\u606f\uff0c\u65b9\u4fbf\u8c03\u8bd5\u548c\u6392\u67e5\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>print(f&quot;Debug: name={name}, age={age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4\u3001\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u53d8\u91cf\u540d\u548c\u5360\u4f4d\u7b26\u5339\u914d\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/li>\n<li>\u6ce8\u610f\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/li>\n<li>\u4f7f\u7528 f-string \u65f6\uff0c\u6ce8\u610f Python \u7248\u672c\u7684\u517c\u5bb9\u6027\u3002<\/li>\n<\/ul>\n<h2><strong>\u516d\u3001\u8fdb\u9636\u7528\u6cd5<\/strong><\/h2>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0cPython \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8fdb\u9636\u7528\u6cd5\uff0c\u53ef\u4ee5\u6ee1\u8db3\u66f4\u590d\u6742\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>datetime<\/code> \u6a21\u5757\u548c f-string \u6765\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>print(f&quot;Current date and time: {now:%Y-%m-%d %H:%M:%S}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u683c\u5f0f\u5316\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u5b57\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 f-string \u7684\u683c\u5f0f\u5316\u9009\u9879\u6765\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pi = 3.141592653589793<\/p>\n<p>print(f&quot;Pi: {pi:.2f}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>:.2f<\/code> \u8868\u793a\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u5d4c\u5957\u683c\u5f0f\u5316<\/h2>\n<\/p>\n<p><p>\u5728\u9700\u8981\u5d4c\u5957\u683c\u5f0f\u5316\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 f-string \u7684\u5d4c\u5957\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>info = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(f&quot;User Info: {info}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8fdb\u9636\u7528\u6cd5\uff0c\u53ef\u4ee5\u770b\u51fa f-string \u7684\u5f3a\u5927\u548c\u7075\u6d3b\u6027\u3002\u65e0\u8bba\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8fd8\u662f\u683c\u5f0f\u5316\u6570\u5b57\u548c\u5d4c\u5957\u683c\u5f0f\u5316\uff0cf-string \u90fd\u80fd\u5f88\u597d\u5730\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\uff0c\u5305\u62ec <code>%<\/code> \u64cd\u4f5c\u7b26\u3001<code>str.format()<\/code> \u65b9\u6cd5\u548c f-string\u3002\u901a\u8fc7\u5bf9\u6bd4\u548c\u603b\u7ed3\uff0c\u53ef\u4ee5\u770b\u51fa f-string \u662f\u76ee\u524d\u6700\u63a8\u8350\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5c24\u5176\u662f\u5728 Python 3.6 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u3002\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u8fd8\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528\uff0c\u5927\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u751f\u6210\u52a8\u6001\u5185\u5bb9\u548c\u8c03\u8bd5\u4fe1\u606f\u65f6\u3002\u901a\u8fc7\u638c\u63e1\u57fa\u672c\u7528\u6cd5\u548c\u8fdb\u9636\u7528\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u5404\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u9700\u6c42\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528 Python \u4e2d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u63d0\u5347\u5f00\u53d1\u6280\u80fd\u548c\u4ee3\u7801\u6c34\u5e73\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u53ef\u4ee5\u5982\u4f55\u4f7f\u7528\u591a\u4e2a\u5360\u4f4d\u7b26\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6216f\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u591a\u4e2a\u5360\u4f4d\u7b26\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u7136\u540e\u5728\u8c03\u7528<code>format()<\/code>\u65f6\u4f20\u5165\u76f8\u5e94\u7684\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">name = &quot;Alice&quot;\nage = 30\nformatted_string = &quot;My name is {} and I am {} years old.&quot;.format(name, age)\n<\/code><\/pre>\n<p>\u4f7f\u7528f\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u5219\u66f4\u4e3a\u7b80\u6d01\uff1a  <\/p>\n<pre><code class=\"language-python\">formatted_string = f&quot;My name is {name} and I am {age} years old.&quot;\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u652f\u6301\u591a\u4e2a\u5360\u4f4d\u7b26\u7684\u4f7f\u7528\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4fdd\u8bc1\u5360\u4f4d\u7b26\u7684\u987a\u5e8f\u548c\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u4f7f\u7528<code>str.format()<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u82b1\u62ec\u53f7\u4e2d\u6307\u5b9a\u6570\u5b57\u6765\u660e\u786e\u5360\u4f4d\u7b26\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">formatted_string = &quot;My name is {0} and I am {1} years old. {0} likes Python.&quot;.format(name, age)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>{0}<\/code>\u548c<code>{1}<\/code>\u5206\u522b\u5bf9\u5e94<code>name<\/code>\u548c<code>age<\/code>\uff0c\u53ef\u4ee5\u7075\u6d3b\u63a7\u5236\u5360\u4f4d\u7b26\u7684\u987a\u5e8f\u548c\u91cd\u590d\u4f7f\u7528\u3002\u800c\u4f7f\u7528f\u5b57\u7b26\u4e32\u65f6\uff0c\u53d8\u91cf\u540d\u672c\u8eab\u5373\u4e3a\u5360\u4f4d\u7b26\uff0c\u786e\u4fdd\u4e86\u6bcf\u4e2a\u53d8\u91cf\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528\u5b57\u5178\u6216\u5217\u8868\u4f5c\u4e3a\u5360\u4f4d\u7b26\u7684\u6570\u636e\u6e90\uff1f<\/strong><br \/>\u5b8c\u5168\u53ef\u4ee5\u3002\u5728Python\u4e2d\uff0c\u5b57\u5178\u548c\u5217\u8868\u90fd\u53ef\u4ee5\u4e0e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7ed3\u5408\u4f7f\u7528\u3002\u5bf9\u4e8e\u5b57\u5178\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728<code>str.format()<\/code>\u4e2d\u4f7f\u7528\u952e\u540d\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}\nformatted_string = &quot;My name is {name} and I am {age} years old.&quot;.format(**data)\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u5217\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u76f8\u5e94\u7684\u5143\u7d20\uff1a  <\/p>\n<pre><code class=\"language-python\">data = [&#39;Alice&#39;, 30]\nformatted_string = &quot;My name is {} and I am {} years old.&quot;.format(data[0], data[1])\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\u4f7f\u7528\u4e24\u4e2a\u5360\u4f4d\u7b26\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528 % \u64cd\u4f5c\u7b26\u3001str.format() \u65b9\u6cd5\u548c f- [&hellip;]","protected":false},"author":3,"featured_media":1047753,"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\/1047742"}],"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=1047742"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047742\/revisions"}],"predecessor-version":[{"id":1047756,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047742\/revisions\/1047756"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1047753"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1047742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1047742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1047742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}