{"id":1125804,"date":"2025-01-08T19:55:06","date_gmt":"2025-01-08T11:55:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125804.html"},"modified":"2025-01-08T19:55:09","modified_gmt":"2025-01-08T11:55:09","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e7%99%be%e5%88%86%e5%8f%b7-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1125804.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8f93\u5165\u767e\u5206\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090333\/60f511af-38cc-4914-9de3-559f13007fbb.webp\" alt=\"python\u4e2d\u5982\u4f55\u8f93\u5165\u767e\u5206\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8f93\u5165\u767e\u5206\u53f7\uff0c\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26%\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3001f-string\u683c\u5f0f\u5316<\/strong>\u7b49\u65b9\u5f0f\u3002\u5728Python\u4e2d\uff0c\u767e\u5206\u53f7\uff08%\uff09\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26\uff0c\u5e38\u7528\u4e8e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u3002\u4e3a\u786e\u4fdd\u767e\u5206\u53f7\u88ab\u6b63\u786e\u89e3\u6790\u5e76\u8f93\u51fa\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u8f6c\u4e49\u5b57\u7b26\u6216\u8005\u9002\u5f53\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<strong>\u8f6c\u4e49\u5b57\u7b26%<\/strong>\u662f\u4e00\u79cd\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u767e\u5206\u53f7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u767e\u5206\u53f7\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u76f4\u63a5\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u767e\u5206\u53f7\uff08%\uff09\u901a\u5e38\u7528\u4e8e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u4f7f\u7528\u767e\u5206\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3002\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u4e24\u4e2a\u8fde\u7eed\u7684\u767e\u5206\u53f7\uff08%%\uff09\u4f1a\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u767e\u5206\u53f7\u3002<\/p>\n<\/p>\n<p><h4>1. \u8f6c\u4e49\u5b57\u7b26\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u4e24\u4e2a\u767e\u5206\u53f7\u8868\u793a\u4e00\u4e2a\u767e\u5206\u53f7<\/p>\n<p>print(&quot;\u8fd9\u662f\u4e00\u4e2a\u767e\u5206\u53f7\uff1a%%&quot; % ())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>&quot;%%&quot;<\/code>\u5c06\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u5355\u4e00\u7684\u767e\u5206\u53f7\u7b26\u53f7\u3002\u8f6c\u4e49\u5b57\u7b26\u662f\u4e00\u79cd\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u8868\u793a\u7279\u6b8a\u5b57\u7b26\u7684\u65b9\u6cd5\u3002\u5728Python\u4e2d\uff0c\u767e\u5206\u53f7\uff08%\uff09\u672c\u8eab\u662f\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\uff0c\u7528\u4e8e\u8868\u793a\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u4f8b\u5982<code>&quot;%s&quot;<\/code>\u3001<code>&quot;%d&quot;<\/code>\u7b49\u3002\u4e3a\u4e86\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u663e\u793a\u767e\u5206\u53f7\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e24\u4e2a\u767e\u5206\u53f7\uff08<code>%%<\/code>\uff09\u6765\u8f6c\u4e49\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u4e14\u5e38\u7528\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528.format()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528.format()\u65b9\u6cd5<\/p>\n<p>percentage = 50<\/p>\n<p>print(&quot;\u8fd9\u4e2a\u6570\u503c\u662f\uff1a{}%&quot;.format(percentage))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>.format()<\/code>\u65b9\u6cd5\u662f\u4e00\u79cd\u66f4\u73b0\u4ee3\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u88abPython 3.x\u5e7f\u6cdb\u4f7f\u7528\u3002\u5b83\u5141\u8bb8\u4f60\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\uff0c\u540c\u65f6\u4fdd\u7559\u683c\u5f0f\u7684\u7075\u6d3b\u6027\u3002\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u767e\u5206\u53f7\u800c\u65e0\u9700\u8f6c\u4e49\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528f-string\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528f-string\u683c\u5f0f\u5316<\/p>\n<p>percentage = 50<\/p>\n<p>print(f&quot;\u8fd9\u4e2a\u6570\u503c\u662f\uff1a{percentage}%&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528<code>f<\/code>\u524d\u7f00\uff0c\u5e76\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u8868\u8fbe\u5f0f\u3002\u4e0e<code>.format()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0cf-string\u4e5f\u4e0d\u9700\u8981\u5bf9\u767e\u5206\u53f7\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u767e\u5206\u53f7\u5728\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u4e2d\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u5728\u751f\u6210\u52a8\u6001\u5185\u5bb9\u65f6\u3002\u767e\u5206\u53f7\u5728Python\u4e2d\u7528\u4e8e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u4f8b\u5982\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;\u59d3\u540d\uff1a%s\uff0c\u5e74\u9f84\uff1a%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>\u548c<code>%d<\/code>\u5206\u522b\u7528\u4e8e\u5b57\u7b26\u4e32\u548c\u6574\u6570\u7684\u683c\u5f0f\u5316\uff0c\u800c<code>%%<\/code>\u8868\u793a\u4e00\u4e2a\u767e\u5206\u53f7\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8be6\u7ec6\u63cf\u8ff0\u8f6c\u4e49\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u8f6c\u4e49\u5b57\u7b26\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u5b57\u7b26\u4e32\u4e2d\u8868\u793a\u65e0\u6cd5\u76f4\u63a5\u8f93\u5165\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5728Python\u4e2d\uff0c<code>\\n<\/code>\u8868\u793a\u6362\u884c\u7b26\uff0c<code>\\t<\/code>\u8868\u793a\u5236\u8868\u7b26\u3002\u540c\u6837\uff0c<code>%%<\/code>\u8868\u793a\u4e00\u4e2a\u767e\u5206\u53f7\u3002\u8f6c\u4e49\u5b57\u7b26\u7684\u4f7f\u7528\u4e0d\u4ec5\u9650\u4e8e\u767e\u5206\u53f7\uff0c\u8fd8\u5305\u62ec\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u5f15\u53f7\u3001\u53cd\u659c\u6760\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5f15\u53f7\u7684\u8f6c\u4e49<\/h4>\n<\/p>\n<p><p>\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u5f15\u53f7\u65f6\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u672c\u8eab\u5305\u542b\u5f15\u53f7\uff0c\u90a3\u4e48\u9700\u8981\u8fdb\u884c\u8f6c\u4e49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u53cc\u5f15\u53f7\u548c\u5355\u5f15\u53f7\u7684\u8f6c\u4e49<\/p>\n<p>quote = &quot;\u5979\u8bf4\uff1a\\&quot;Python\u7f16\u7a0b\u5f88\u6709\u8da3\uff01\\&quot;&quot;<\/p>\n<p>print(quote)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u53cd\u659c\u6760\uff08<code><\/code>\uff09\u7528\u4e8e\u8f6c\u4e49\u53cc\u5f15\u53f7\uff0c\u4f7f\u5176\u80fd\u591f\u6b63\u786e\u5730\u663e\u793a\u5728\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u53cd\u659c\u6760\u7684\u8f6c\u4e49<\/h4>\n<\/p>\n<p><p>\u53cd\u659c\u6760\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\uff0c\u7528\u4e8e\u8f6c\u4e49\u5176\u4ed6\u5b57\u7b26\u3002\u5982\u679c\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u663e\u793a\u53cd\u659c\u6760\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cc\u53cd\u659c\u6760\uff08<code>\\<\/code>\uff09\u8fdb\u884c\u8f6c\u4e49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u53cc\u53cd\u659c\u6760\u8868\u793a\u4e00\u4e2a\u53cd\u659c\u6760<\/p>\n<p>path = &quot;C:\\\\Users\\\\Alice\\\\Documents&quot;<\/p>\n<p>print(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>\\<\/code>\u5c06\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u53cd\u659c\u6760\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u7684\u8be6\u7ec6\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u5728Python\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001<code>.format()<\/code>\u65b9\u6cd5\u548cf-string\u683c\u5f0f\u5316\u3002\u4ee5\u4e0b\u662f\u8fd9\u4e9b\u65b9\u6cd5\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h4>1. \u767e\u5206\u53f7\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u662fPython\u65e9\u671f\u7248\u672c\u4e2d\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528<code>%<\/code>\u64cd\u4f5c\u7b26\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u767e\u5206\u53f7\u683c\u5f0f\u5316<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;\u59d3\u540d\uff1a%s\uff0c\u5e74\u9f84\uff1a%d&quot; % (name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u76f4\u89c2\u4e14\u7b80\u5355\uff0c\u4f46\u4e0d\u591f\u7075\u6d3b\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u590d\u6742\u683c\u5f0f\u65f6\u3002<\/p>\n<\/p>\n<p><h4>2. .format()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>.format()<\/code>\u65b9\u6cd5\u662fPython 3.x\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u66f4\u7075\u6d3b\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># .format()\u65b9\u6cd5<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;\u59d3\u540d\uff1a{}\uff0c\u5e74\u9f84\uff1a{}&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>.format()<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f7f\u7528\u547d\u540d\u53c2\u6570\u3001\u7d22\u5f15\u7b49\uff0c\u4f7f\u5f97\u683c\u5f0f\u5316\u64cd\u4f5c\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>3. f-string\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>f-string\u683c\u5f0f\u5316\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528<code>f<\/code>\u524d\u7f00\uff0c\u5e76\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># f-string\u683c\u5f0f\u5316<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(f&quot;\u59d3\u540d\uff1a{name}\uff0c\u5e74\u9f84\uff1a{age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-string\u683c\u5f0f\u5316\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6027\u80fd\u66f4\u9ad8\uff0c\u662f\u63a8\u8350\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u793a\u4f8b\u548c\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u975e\u5e38\u5e38\u89c1\uff0c\u7279\u522b\u662f\u5728\u751f\u6210\u52a8\u6001\u5185\u5bb9\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u793a\u4f8b\u548c\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><h4>1. \u751f\u6210\u52a8\u6001\u7f51\u9875\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u751f\u6210\u52a8\u6001\u7f51\u9875\u5185\u5bb9\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u63d2\u5165\u52a8\u6001\u6570\u636e\uff0c\u4f8b\u5982\u7528\u6237\u8f93\u5165\u3001\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u751f\u6210\u52a8\u6001\u7f51\u9875\u5185\u5bb9<\/p>\n<p>user = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}<\/p>\n<p>html = f&quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;&lt;title&gt;\u7528\u6237\u4fe1\u606f&lt;\/title&gt;&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;h1&gt;\u59d3\u540d\uff1a{user[&#39;name&#39;]}&lt;\/h1&gt;<\/p>\n<p>&lt;p&gt;\u5e74\u9f84\uff1a{user[&#39;age&#39;]}%&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528f-string\u683c\u5f0f\u5316\u751f\u6210\u4e86\u4e00\u6bb5\u5305\u542b\u52a8\u6001\u6570\u636e\u7684HTML\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. \u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u65e5\u5fd7\u8bb0\u5f55\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u5305\u542b\u52a8\u6001\u6570\u636e\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u8bb0\u5f55\u65e5\u5fd7<\/p>\n<p>import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>user = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}<\/p>\n<p>logging.info(&quot;\u7528\u6237\u4fe1\u606f\uff1a\u59d3\u540d=%s\uff0c\u5e74\u9f84=%d%%&quot;, user[&#39;name&#39;], user[&#39;age&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316\u751f\u6210\u4e86\u4e00\u6761\u5305\u542b\u52a8\u6001\u6570\u636e\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>3. \u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u8fc7\u7a0b\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u5305\u542b\u52a8\u6001\u6570\u636e\u7684\u6807\u9898\u3001\u6807\u7b7e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u751f\u6210\u6570\u636e\u5206\u6790\u62a5\u544a<\/p>\n<p>data = {&quot;mean&quot;: 75.5, &quot;std_dev&quot;: 10.2}<\/p>\n<p>report = f&quot;&quot;&quot;<\/p>\n<p>\u6570\u636e\u5206\u6790\u62a5\u544a<\/p>\n<p>--------------<\/p>\n<p>\u5747\u503c\uff1a{data[&#39;mean&#39;]}%<\/p>\n<p>\u6807\u51c6\u5dee\uff1a{data[&#39;std_dev&#39;]}%<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>print(report)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528f-string\u683c\u5f0f\u5316\u751f\u6210\u4e86\u4e00\u4efd\u5305\u542b\u52a8\u6001\u6570\u636e\u7684\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u767e\u5206\u53f7\uff08%\uff09\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26\uff0c\u5e38\u7528\u4e8e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u3002\u4e3a\u4e86\u5728\u5b57\u7b26\u4e32\u4e2d\u6b63\u786e\u663e\u793a\u767e\u5206\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\uff08%%\uff09\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff08\u4f8b\u5982<code>.format()<\/code>\u65b9\u6cd5\u548cf-string\u683c\u5f0f\u5316\uff09\u3002\u8f6c\u4e49\u5b57\u7b26\u4e0d\u4ec5\u9650\u4e8e\u767e\u5206\u53f7\uff0c\u8fd8\u5305\u62ec\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u5f15\u53f7\u3001\u53cd\u659c\u6760\u7b49\u3002\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5176\u4e2df-string\u683c\u5f0f\u5316\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u3001\u63a8\u8350\u4f7f\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u793a\u4f8b\u548c\u5b9e\u8df5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8868\u793a\u767e\u5206\u6bd4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6d6e\u70b9\u6570\u6765\u8868\u793a\u767e\u5206\u6bd4\u3002\u4f8b\u5982\uff0c50%\u7684\u8868\u8fbe\u65b9\u5f0f\u53ef\u4ee5\u5199\u62100.5\u3002\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u767e\u5206\u6bd4\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>&quot;{:.2%}&quot;.format(0.5)<\/code>\u4f1a\u8f93\u51fa\u201c50.00%\u201d\u3002\u8fd9\u79cd\u683c\u5f0f\u5316\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4ee5\u767e\u5206\u6bd4\u5f62\u5f0f\u5c55\u793a\u7684\u6570\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5e26\u6709\u767e\u5206\u53f7\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u767e\u5206\u53f7\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>&quot;50% completed&quot;<\/code>\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u9700\u8981\u5bf9\u5305\u542b\u767e\u5206\u53f7\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u8ba1\u7b97\uff0c\u5efa\u8bae\u5148\u53bb\u6389\u767e\u5206\u53f7\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u6570\u5f62\u5f0f\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u767e\u5206\u6bd4\u683c\u5f0f\uff1f<\/strong><br \/>\u8981\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u767e\u5206\u6bd4\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e58\u6cd5\u548c\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5c0f\u65700.25\uff0c\u53ef\u4ee5\u901a\u8fc7<code>percentage = 0.25 * 100<\/code>\u5f97\u523025\uff0c\u7136\u540e\u4f7f\u7528<code>print(f&quot;{percentage}%&quot;)<\/code>\u6765\u8f93\u51fa\u201c25%\u201d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u975e\u5e38\u9002\u5408\u5728\u6570\u636e\u5c55\u793a\u4e2d\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u5165\u767e\u5206\u53f7\uff0c\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26%\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3001f-string\u683c\u5f0f\u5316\u7b49\u65b9\u5f0f\u3002\u5728Python\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1125816,"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\/1125804"}],"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=1125804"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125804\/revisions"}],"predecessor-version":[{"id":1125818,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125804\/revisions\/1125818"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1125816"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}