{"id":1062960,"date":"2024-12-31T15:56:34","date_gmt":"2024-12-31T07:56:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1062960.html"},"modified":"2024-12-31T15:56:37","modified_gmt":"2024-12-31T07:56:37","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%a0%bc%e5%bc%8f%e5%8c%96%e8%be%93%e5%87%ba%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1062960.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\u8bed\u53e5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1fb147a8-8c0a-45d8-98ac-134979faf4b7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\u8bed\u53e5\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u683c\u5f0f\u5316\u8f93\u51fa\u8bed\u53e5\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09\u3001\u4f7f\u7528str.format()\u65b9\u6cd5\u3001\u4f7f\u7528f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u3002<\/strong> \u5176\u4e2d\uff0c<strong>f-string<\/strong> \u662f\u76ee\u524d\u6700\u63a8\u8350\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u66f4\u7b80\u6d01\u3001\u76f4\u89c2\u4e14\u529f\u80fd\u5f3a\u5927\u3002<strong>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09<\/strong>\u662f\u4e00\u79cd\u8f83\u4e3a\u4f20\u7edf\u7684\u65b9\u5f0f\uff0c\u867d\u7136\u4f9d\u65e7\u6709\u6548\uff0c\u4f46\u8f83\u4e3a\u7e41\u7410\uff0c<strong>str.format()\u65b9\u6cd5<\/strong>\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u4f46\u8bed\u6cd5\u76f8\u5bf9\u590d\u6742\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/strong><\/p>\n<\/p>\n<p><p>f-string \u662f Python 3.6 \u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u5c06\u53d8\u91cf\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002f-string \u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd\u201cf\u201d\u6216\u201cF\u201d\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u5927\u62ec\u53f7\u201c{}\u201d\u5d4c\u5165\u53d8\u91cf\u540d\u6216\u8868\u8fbe\u5f0f\uff0c\u4ece\u800c\u5b9e\u73b0\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/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\u4ee3\u7801\u4e2d\uff0c<code>f&quot;Name: {name}, Age: {age}&quot;<\/code> \u662f\u4e00\u4e2a f-string\uff0c<code>{name}<\/code> \u548c <code>{age}<\/code> \u4f1a\u88ab\u76f8\u5e94\u7684\u53d8\u91cf\u503c\u66ff\u6362\uff0c\u4ece\u800c\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\u3002<strong>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u652f\u6301\u5728\u5927\u62ec\u53f7\u5185\u8fdb\u884c\u8868\u8fbe\u5f0f\u8fd0\u7b97\u3001\u8c03\u7528\u51fd\u6570\u7b49\u64cd\u4f5c\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7075\u6d3b\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u662f Python \u4e2d\u6700\u65e9\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528 <code>%<\/code> \u7b26\u53f7\u6307\u5b9a\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u7136\u540e\u5728\u5b57\u7b26\u4e32\u672b\u5c3e\u4f7f\u7528 <code>%<\/code> \u7b26\u53f7\u548c\u4e00\u4e2a\u5143\u7ec4\u6765\u6307\u5b9a\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/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\u4ee3\u7801\u4e2d\uff0c<code>%s<\/code> \u8868\u793a\u5b57\u7b26\u4e32\uff0c<code>%d<\/code> \u8868\u793a\u6574\u6570\uff0c<code>%<\/code> \u540e\u9762\u7684\u5143\u7ec4 <code>(name, age)<\/code> \u4f1a\u4f9d\u6b21\u66ff\u6362\u5bf9\u5e94\u7684\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u4ece\u800c\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\u3002<strong>\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u683c\u5f0f\u5316\u9700\u6c42\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>str.format()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u662f Python 2.7 \u548c 3.0 \u5f15\u5165\u7684\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\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\u201c{}\u201d\u6307\u5b9a\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u7136\u540e\u5728 <code>format<\/code> \u65b9\u6cd5\u4e2d\u4f20\u5165\u76f8\u5e94\u7684\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/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\u4ee3\u7801\u4e2d\uff0c<code>{}<\/code> \u662f\u683c\u5f0f\u5316\u6807\u8bb0\uff0c<code>format(name, age)<\/code> \u4e2d\u7684\u53d8\u91cf\u503c\u4f1a\u4f9d\u6b21\u66ff\u6362\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u4ece\u800c\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\u3002<strong>\u4e0e\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u76f8\u6bd4\uff0cstr.format() \u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u5982\u6307\u5b9a\u4f4d\u7f6e\u3001\u6307\u5b9a\u5173\u952e\u5b57\u53c2\u6570\u3001\u6307\u5b9a\u683c\u5f0f\u5316\u7c7b\u578b\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/strong><\/p>\n<\/p>\n<p><p>f-string \u662f Python 3.6 \u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u5c06\u53d8\u91cf\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002f-string \u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd\u201cf\u201d\u6216\u201cF\u201d\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u5927\u62ec\u53f7\u201c{}\u201d\u5d4c\u5165\u53d8\u91cf\u540d\u6216\u8868\u8fbe\u5f0f\uff0c\u4ece\u800c\u5b9e\u73b0\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/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\u4ee3\u7801\u4e2d\uff0c<code>f&quot;Name: {name}, Age: {age}&quot;<\/code> \u662f\u4e00\u4e2a f-string\uff0c<code>{name}<\/code> \u548c <code>{age}<\/code> \u4f1a\u88ab\u76f8\u5e94\u7684\u53d8\u91cf\u503c\u66ff\u6362\uff0c\u4ece\u800c\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u652f\u6301\u5728\u5927\u62ec\u53f7\u5185\u8fdb\u884c\u8868\u8fbe\u5f0f\u8fd0\u7b97\u3001\u8c03\u7528\u51fd\u6570\u7b49\u64cd\u4f5c\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528<\/h3>\n<\/p>\n<p><p>f-string \u652f\u6301\u5728\u5927\u62ec\u53f7\u5185\u8fdb\u884c\u5404\u79cd\u8868\u8fbe\u5f0f\u8fd0\u7b97\u548c\u51fd\u6570\u8c03\u7528\uff0c\u4f7f\u5f97\u683c\u5f0f\u5316\u8f93\u51fa\u529f\u80fd\u66f4\u52a0\u5f3a\u5927\u548c\u7075\u6d3b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u5927\u62ec\u53f7\u5185\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">width = 10<\/p>\n<p>height = 5<\/p>\n<p>print(f&quot;Area: {width * height}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Area: 50<\/code>\uff0c\u56e0\u4e3a\u5728\u5927\u62ec\u53f7\u5185\u8fdb\u884c\u4e86\u4e58\u6cd5\u8fd0\u7b97\u3002\u540c\u6837\uff0c\u53ef\u4ee5\u5728\u5927\u62ec\u53f7\u5185\u8c03\u7528\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>radius = 5<\/p>\n<p>print(f&quot;Circle Area: {math.pi * radius2:.2f}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Circle Area: 78.54<\/code>\uff0c\u56e0\u4e3a\u5728\u5927\u62ec\u53f7\u5185\u8c03\u7528\u4e86 <code>math.pi<\/code> \u548c <code>radius2<\/code> \u8868\u8fbe\u5f0f\uff0c\u5e76\u4f7f\u7528 <code>:.2f<\/code> \u6307\u5b9a\u4e86\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u683c\u5f0f\u5316\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>f-string \u652f\u6301\u591a\u79cd\u683c\u5f0f\u5316\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5927\u62ec\u53f7\u5185\u7684\u683c\u5f0f\u8bf4\u660e\u7b26\u6307\u5b9a\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 123.456<\/p>\n<p>print(f&quot;Formatted Value: {value:.2f}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Formatted Value: 123.46<\/code>\uff0c\u56e0\u4e3a\u4f7f\u7528 <code>:.2f<\/code> \u6307\u5b9a\u4e86\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u683c\u5f0f\u8bf4\u660e\u7b26\uff0c\u5982 <code>d<\/code> \u8868\u793a\u6574\u6570\uff0c<code>s<\/code> \u8868\u793a\u5b57\u7b26\u4e32\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 42<\/p>\n<p>print(f&quot;Number: {number:d}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Number: 42<\/code>\uff0c\u56e0\u4e3a\u4f7f\u7528 <code>:d<\/code> \u6307\u5b9a\u4e86\u6574\u6570\u683c\u5f0f\u5316\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u662f Python \u4e2d\u6700\u65e9\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528 <code>%<\/code> \u7b26\u53f7\u6307\u5b9a\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u7136\u540e\u5728\u5b57\u7b26\u4e32\u672b\u5c3e\u4f7f\u7528 <code>%<\/code> \u7b26\u53f7\u548c\u4e00\u4e2a\u5143\u7ec4\u6765\u6307\u5b9a\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/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\u4ee3\u7801\u4e2d\uff0c<code>%s<\/code> \u8868\u793a\u5b57\u7b26\u4e32\uff0c<code>%d<\/code> \u8868\u793a\u6574\u6570\uff0c<code>%<\/code> \u540e\u9762\u7684\u5143\u7ec4 <code>(name, age)<\/code> \u4f1a\u4f9d\u6b21\u66ff\u6362\u5bf9\u5e94\u7684\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u4ece\u800c\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5e38\u89c1\u683c\u5f0f\u5316\u6807\u8bb0<\/h3>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u652f\u6301\u591a\u79cd\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u5e38\u89c1\u7684\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>%s<\/code>\uff1a\u5b57\u7b26\u4e32<\/li>\n<li><code>%d<\/code>\uff1a\u6574\u6570<\/li>\n<li><code>%f<\/code>\uff1a\u6d6e\u70b9\u6570<\/li>\n<li><code>%x<\/code>\uff1a\u5341\u516d\u8fdb\u5236\u6574\u6570<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>%f<\/code> \u683c\u5f0f\u5316\u6d6e\u70b9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 123.456<\/p>\n<p>print(&quot;Formatted Value: %.2f&quot; % value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Formatted Value: 123.46<\/code>\uff0c\u56e0\u4e3a\u4f7f\u7528 <code>%.2f<\/code> \u6307\u5b9a\u4e86\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>%x<\/code> \u683c\u5f0f\u5316\u5341\u516d\u8fdb\u5236\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 255<\/p>\n<p>print(&quot;Hex Number: %x&quot; % number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Hex Number: ff<\/code>\uff0c\u56e0\u4e3a\u4f7f\u7528 <code>%x<\/code> \u6307\u5b9a\u4e86\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u5316\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e0d\u8db3\u4e0e\u5c40\u9650<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u63d0\u4f9b\u4e86\u4e00\u5b9a\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u4f46\u5b83\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u4e0d\u5982\u5176\u4ed6\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u590d\u6742\u7684\u683c\u5f0f\u5316\u9700\u6c42\u65f6\uff0c\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\u3002\u540c\u65f6\uff0c\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u7684\u8bed\u6cd5\u8f83\u4e3a\u7e41\u7410\uff0c\u4e0d\u5229\u4e8e\u4ee3\u7801\u7684\u7ef4\u62a4\u548c\u9605\u8bfb\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u4f9d\u65e7\u6709\u6548\uff0c\u4f46\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u66f4\u63a8\u8350\u4f7f\u7528\u5176\u4ed6\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5982 f-string \u548c str.format() \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001str.format()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u662f Python 2.7 \u548c 3.0 \u5f15\u5165\u7684\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\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\u201c{}\u201d\u6307\u5b9a\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u7136\u540e\u5728 <code>format<\/code> \u65b9\u6cd5\u4e2d\u4f20\u5165\u76f8\u5e94\u7684\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/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\u4ee3\u7801\u4e2d\uff0c<code>{}<\/code> \u662f\u683c\u5f0f\u5316\u6807\u8bb0\uff0c<code>format(name, age)<\/code> \u4e2d\u7684\u53d8\u91cf\u503c\u4f1a\u4f9d\u6b21\u66ff\u6362\u683c\u5f0f\u5316\u6807\u8bb0\uff0c\u4ece\u800c\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u6307\u5b9a\u4f4d\u7f6e\u548c\u5173\u952e\u5b57\u53c2\u6570<\/h3>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u652f\u6301\u901a\u8fc7\u4f4d\u7f6e\u548c\u5173\u952e\u5b57\u53c2\u6570\u6307\u5b9a\u683c\u5f0f\u5316\u6807\u8bb0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f4d\u7f6e\u53c2\u6570\u6307\u5b9a\u683c\u5f0f\u5316\u6807\u8bb0\u7684\u987a\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: {0}, Age: {1}&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\uff0c\u56e0\u4e3a <code>{0}<\/code> \u548c <code>{1}<\/code> \u5206\u522b\u5bf9\u5e94 <code>format<\/code> \u65b9\u6cd5\u4e2d\u7684\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\u540c\u6837\uff0c\u53ef\u4ee5\u901a\u8fc7\u5173\u952e\u5b57\u53c2\u6570\u6307\u5b9a\u683c\u5f0f\u5316\u6807\u8bb0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Name: {name}, Age: {age}&quot;.format(name=&quot;Alice&quot;, age=30))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\uff0c\u56e0\u4e3a <code>{name}<\/code> \u548c <code>{age}<\/code> \u5206\u522b\u5bf9\u5e94 <code>format<\/code> \u65b9\u6cd5\u4e2d\u7684 <code>name<\/code> \u548c <code>age<\/code> \u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5d4c\u5957\u548c\u590d\u6742\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u652f\u6301\u5d4c\u5957\u548c\u590d\u6742\u7684\u683c\u5f0f\u5316\u9700\u6c42\uff0c\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u683c\u5f0f\u5316\u6807\u8bb0\u4e2d\u5d4c\u5957\u5176\u4ed6\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}<\/p>\n<p>print(&quot;Name: {0[name]}, Age: {0[age]}&quot;.format(person))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\uff0c\u56e0\u4e3a <code>{0[name]}<\/code> \u548c <code>{0[age]}<\/code> \u5206\u522b\u5bf9\u5e94 <code>person<\/code> \u5b57\u5178\u4e2d\u7684 <code>name<\/code> \u548c <code>age<\/code> \u952e\u3002\u540c\u6837\uff0c\u53ef\u4ee5\u6307\u5b9a\u683c\u5f0f\u5316\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 123.456<\/p>\n<p>print(&quot;Formatted Value: {:.2f}&quot;.format(value))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Formatted Value: 123.46<\/code>\uff0c\u56e0\u4e3a\u4f7f\u7528 <code>:.2f<\/code> \u6307\u5b9a\u4e86\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e0d\u8db3\u4e0e\u5c40\u9650<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1str.format()\u65b9\u6cd5\u63d0\u4f9b\u4e86\u6bd4\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u4f46\u5176\u8bed\u6cd5\u76f8\u5bf9\u590d\u6742\uff0c\u4e0d\u5982 f-string \u76f4\u89c2\u548c\u7b80\u6d01\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5d4c\u5957\u548c\u590d\u6742\u7684\u683c\u5f0f\u5316\u9700\u6c42\u65f6\uff0cstr.format()\u65b9\u6cd5\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1str.format()\u65b9\u6cd5\u4f9d\u65e7\u6709\u6548\uff0c\u4f46\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u66f4\u63a8\u8350\u4f7f\u7528 f-string\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u5176\u4ed6\u683c\u5f0f\u5316\u65b9\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u4e3b\u8981\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u5916\uff0cPython \u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u4e00\u4e9b\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u7684\u8fde\u63a5\u548c\u62fc\u63a5\u3001\u6a21\u677f\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u5b57\u7b26\u4e32\u7684\u8fde\u63a5\u548c\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u7684\u8fde\u63a5\u548c\u62fc\u63a5\u5b9e\u73b0\u7b80\u5355\u7684\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f8b\u5982\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fde\u63a5\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: &quot; + name + &quot;, Age: &quot; + str(age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\uff0c\u56e0\u4e3a\u4f7f\u7528\u52a0\u53f7\u8fde\u63a5\u4e86\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u503c\u3002\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\uff08,\uff09\u62fc\u63a5\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Name:&quot;, name, &quot;, Age:&quot;, age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Name: Alice , Age: 30<\/code>\uff0c\u56e0\u4e3a\u4f7f\u7528\u9017\u53f7\u62fc\u63a5\u4e86\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u6a21\u677f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6a21\u677f\u5b57\u7b26\u4e32\u662f Python \u6807\u51c6\u5e93\u4e2d\u7684 <code>string.Template<\/code> \u7c7b\u63d0\u4f9b\u7684\u4e00\u79cd\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528 <code>$<\/code> \u7b26\u53f7\u6307\u5b9a\u5360\u4f4d\u7b26\uff0c\u7136\u540e\u8c03\u7528 <code>substitute<\/code> \u65b9\u6cd5\u66ff\u6362\u5360\u4f4d\u7b26\u4e3a\u76f8\u5e94\u7684\u53d8\u91cf\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>template = Template(&quot;Name: $name, Age: $age&quot;)<\/p>\n<p>print(template.substitute(name=&quot;Alice&quot;, age=30))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>Name: Alice, Age: 30<\/code>\uff0c\u56e0\u4e3a <code>substitute<\/code> \u65b9\u6cd5\u5c06 <code>$name<\/code> \u548c <code>$age<\/code> \u5360\u4f4d\u7b26\u66ff\u6362\u4e3a\u76f8\u5e94\u7684\u53d8\u91cf\u503c\u3002\u6a21\u677f\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4e00\u4e9b\u7279\u5b9a\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u6bd4\u8f83\u4e0e\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u5982\u679c\u9700\u8981\u7b80\u6d01\u3001\u76f4\u89c2\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u63a8\u8350\u4f7f\u7528 f-string\uff1b\u5982\u679c\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u683c\u5f0f\u5316\u9700\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 str.format() \u65b9\u6cd5\uff1b\u5982\u679c\u9700\u8981\u517c\u5bb9\u65e7\u7248\u672c\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u683c\u5f0f\u5316\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u8fde\u63a5\u548c\u62fc\u63a5\uff1b\u5bf9\u4e8e\u4e00\u4e9b\u7279\u5b9a\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u683c\u5f0f\u5316\u8f93\u51fa\u8bed\u53e5\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09\u3001str.format()\u65b9\u6cd5\u548cf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u3002\u5176\u4e2d\uff0cf-string \u662f\u76ee\u524d\u6700\u63a8\u8350\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u66f4\u7b80\u6d01\u3001\u76f4\u89c2\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u662f\u4e00\u79cd\u8f83\u4e3a\u4f20\u7edf\u7684\u65b9\u5f0f\uff0c\u867d\u7136\u4f9d\u65e7\u6709\u6548\uff0c\u4f46\u8f83\u4e3a\u7e41\u7410\u3002str.format()\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u4f46\u8bed\u6cd5\u76f8\u5bf9\u590d\u6742\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\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\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u7b26\u4e32\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528f-string\u3001str.format()\u65b9\u6cd5\u548c\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316\u3002f-string\u662fPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<code>name = &quot;Alice&quot;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>print(f&quot;Hello, {name}&quot;)<\/code>\u8f93\u51fa<code>Hello, Alice<\/code>\u3002\u800c\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7\u5360\u4f4d\u7b26\uff0c\u5982<code>print(&quot;Hello, {}&quot;.format(name))<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u8fdb\u884c\u6570\u5b57\u683c\u5f0f\u5316\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u5b57\u683c\u5f0f\u5316\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u683c\u5f0f\u8bf4\u660e\u7b26\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528<code>:.2f<\/code>\u6765\u9650\u5236\u6d6e\u70b9\u6570\u7684\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002\u793a\u4f8b\u4ee3\u7801<code>value = 3.14159<\/code>\uff0c\u8f93\u51fa\u65f6\u53ef\u4ee5\u4f7f\u7528<code>print(f&quot;Value: {value:.2f}&quot;)<\/code>\uff0c\u7ed3\u679c\u5c06\u663e\u793a\u4e3a<code>Value: 3.14<\/code>\u3002\u6b64\u5916\uff0c\u683c\u5f0f\u5316\u8f93\u51fa\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e8e\u6574\u6570\uff0c\u6bd4\u5982\u901a\u8fc7<code>{:,.0f}<\/code>\u6765\u6dfb\u52a0\u5343\u4f4d\u5206\u9694\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\uff1f<\/strong><br \/>Python\u7684<code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u6bd4\u5982\uff0c<code>from datetime import datetime<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>now = datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u63a5\u7740\u901a\u8fc7<code>formatted_date = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/code>\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u4e3a<code>YYYY-MM-DD HH:MM:SS<\/code>\u7684\u5f62\u5f0f\u3002\u8fd9\u4f7f\u5f97\u5728\u8f93\u51fa\u65e5\u5fd7\u6216\u663e\u793a\u7528\u6237\u53cb\u597d\u7684\u65e5\u671f\u683c\u5f0f\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u683c\u5f0f\u5316\u8f93\u51fa\u8bed\u53e5\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09\u3001\u4f7f\u7528str.format()\u65b9\u6cd5\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1062971,"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\/1062960"}],"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=1062960"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062960\/revisions"}],"predecessor-version":[{"id":1062975,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062960\/revisions\/1062975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1062971"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1062960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1062960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1062960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}