{"id":1133995,"date":"2025-01-08T21:11:57","date_gmt":"2025-01-08T13:11:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133995.html"},"modified":"2025-01-08T21:12:00","modified_gmt":"2025-01-08T13:12:00","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e7%99%be%e5%88%86%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133995.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u663e\u793a\u767e\u5206\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103043\/02646056-9873-41af-94e1-c8e07f873175.webp\" alt=\"python\u4e2d\u5982\u4f55\u663e\u793a\u767e\u5206\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u663e\u793a\u767e\u5206\u6570\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f7f\u7528f-string\u3001\u501f\u52a9\u683c\u5f0f\u5316\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528f-string\u662f\u6700\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u901a\u8fc7f-string\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u767e\u5206\u6570\u8868\u793a\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528f-string\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\uff0c\u5e76\u4e14\u652f\u6301\u683c\u5f0f\u5316\u64cd\u4f5c\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>:.2%<\/code>\u8fd9\u6837\u7684\u683c\u5f0f\u5316\u6307\u4ee4\uff0c\u5c06\u6d6e\u70b9\u6570\u8f6c\u5316\u4e3a\u767e\u5206\u6570\u5e76\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 0.1234<\/p>\n<p>formatted_value = f&quot;{value:.2%}&quot;<\/p>\n<p>print(formatted_value)  # \u8f93\u51fa\uff1a12.34%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u975e\u5e38\u76f4\u89c2\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0cf-string\u7684\u4f7f\u7528\u9891\u7387\u975e\u5e38\u9ad8\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python\u4e2d\u663e\u793a\u767e\u5206\u6570\u7684\u4e0d\u540c\u65b9\u6cd5\uff0c\u5206\u6790\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u662fPython\u4e2d\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u7279\u5b9a\u7684\u683c\u5f0f\u5316\u7b26\u53f7\u5c06\u53d8\u91cf\u5d4c\u5165\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u57fa\u7840\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728Python 3.6\u4e4b\u524d\uff0c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u5e38\u89c1\u7528\u6cd5\u662f\u901a\u8fc7<code>%<\/code>\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 0.1234<\/p>\n<p>formatted_value = &quot;%.2f%%&quot; % (value * 100)<\/p>\n<p>print(formatted_value)  # \u8f93\u51fa\uff1a12.34%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>%.2f<\/code>\u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u6d6e\u70b9\u6570\uff0c<code>%%<\/code>\u5219\u8868\u793a\u4e00\u4e2a\u767e\u5206\u53f7\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528str.format()<\/h2>\n<\/p>\n<p><p>\u5728Python 3.0\u4e4b\u540e\uff0c<code>str.format()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 0.1234<\/p>\n<p>formatted_value = &quot;{:.2f}%&quot;.format(value * 100)<\/p>\n<p>print(formatted_value)  # \u8f93\u51fa\uff1a12.34%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>{:.2f}%<\/code>\u4e2d\u7684<code>:.2f<\/code>\u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u6d6e\u70b9\u6570\uff0c<code>%<\/code>\u8868\u793a\u767e\u5206\u53f7\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/strong><\/h2>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u7b80\u5316\u4e86\u4e4b\u524d\u7684\u683c\u5f0f\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u57fa\u7840\u7528\u6cd5<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">value = 0.1234<\/p>\n<p>formatted_value = f&quot;{value:.2%}&quot;<\/p>\n<p>print(formatted_value)  # \u8f93\u51fa\uff1a12.34%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>:.2%<\/code>\u8868\u793a\u5c06\u6d6e\u70b9\u6570\u8f6c\u5316\u4e3a\u767e\u5206\u6570\u5e76\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u590d\u6742\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>f-string\u4e0d\u4ec5\u53ef\u4ee5\u5d4c\u5165\u7b80\u5355\u7684\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u5d4c\u5165\u590d\u6742\u7684\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 0.1234<\/p>\n<p>formatted_value = f&quot;{value * 100:.2f}%&quot;<\/p>\n<p>print(formatted_value)  # \u8f93\u51fa\uff1a12.34%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u9700\u8981\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\u7684\u573a\u666f\u4e0b\u5c24\u5176\u6709\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u501f\u52a9\u683c\u5f0f\u5316\u51fd\u6570<\/strong><\/h2>\n<p><p>Python\u4e2d\u8fd8\u6709\u4e00\u4e9b\u5185\u7f6e\u7684\u683c\u5f0f\u5316\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u663e\u793a\u767e\u5206\u6570\u3002<\/p>\n<\/p>\n<p><h2>1\u3001format()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>format()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 0.1234<\/p>\n<p>formatted_value = format(value, &quot;.2%&quot;)<\/p>\n<p>print(formatted_value)  # \u8f93\u51fa\uff1a12.34%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001locale\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>locale<\/code>\u6a21\u5757\u53ef\u4ee5\u6839\u636e\u5730\u533a\u8bbe\u7f6e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u9002\u7528\u4e8e\u9700\u8981\u56fd\u9645\u5316\u7684\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import locale<\/p>\n<p>locale.setlocale(locale.LC_ALL, &#39;&#39;)<\/p>\n<p>value = 0.1234<\/p>\n<p>formatted_value = locale.format_string(&quot;%.2f%%&quot;, value * 100)<\/p>\n<p>print(formatted_value)  # \u8f93\u51fa\uff1a12.34%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>locale.format_string()<\/code>\u65b9\u6cd5\u6839\u636e\u5f53\u524d\u7684\u5730\u533a\u8bbe\u7f6e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n<p><h2>1\u3001\u91d1\u878d\u6570\u636e\u5c55\u793a<\/h2>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u6570\u636e\u5c55\u793a\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u4ee5\u767e\u5206\u6bd4\u5f62\u5f0f\u5c55\u793a\u6570\u636e\uff0c\u6bd4\u5982\u5229\u7387\u3001\u589e\u957f\u7387\u7b49\u3002\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u6d6e\u70b9\u6570\u8f6c\u5316\u4e3a\u767e\u5206\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">interest_rate = 0.035<\/p>\n<p>print(f&quot;The interest rate is {interest_rate:.2%}&quot;)  # \u8f93\u51fa\uff1aThe interest rate is 3.50%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u6570\u636e\u5206\u6790\u62a5\u544a<\/h2>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u62a5\u544a\u4e2d\uff0c\u767e\u5206\u6bd4\u662f\u975e\u5e38\u5e38\u89c1\u7684\u8868\u793a\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5c55\u793a\u67d0\u4ea7\u54c1\u7684\u5e02\u573a\u4efd\u989d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">market_share = 0.25<\/p>\n<p>print(f&quot;The product&#39;s market share is {market_share:.1%}&quot;)  # \u8f93\u51fa\uff1aThe product&#39;s market share is 25.0%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u8003\u8bd5\u6210\u7ee9\u7edf\u8ba1<\/h2>\n<\/p>\n<p><p>\u5728\u8003\u8bd5\u6210\u7ee9\u7edf\u8ba1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u5206\u6570\u8f6c\u5316\u4e3a\u767e\u5206\u6bd4\u5f62\u5f0f\u5c55\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">score = 0.89<\/p>\n<p>print(f&quot;The student&#39;s score is {score:.2%}&quot;)  # \u8f93\u51fa\uff1aThe student&#39;s score is 89.00%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\u663e\u793a\u767e\u5206\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u3002<strong>\u4f7f\u7528f-string\u662f\u6700\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5d4c\u5165\u7b80\u5355\u7684\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u5d4c\u5165\u590d\u6742\u7684\u8868\u8fbe\u5f0f\uff0c\u975e\u5e38\u9002\u5408\u5728\u65e5\u5e38\u7f16\u7801\u4e2d\u4f7f\u7528\u3002\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c<code>str.format()<\/code>\u65b9\u6cd5\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u901a\u8fc7\u501f\u52a9\u5185\u7f6e\u7684\u683c\u5f0f\u5316\u51fd\u6570\u548c<code>locale<\/code>\u6a21\u5757\uff0c\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u56fd\u9645\u5316\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u5173\u952e\u662f\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u6700\u5408\u9002\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\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\u683c\u5f0f\u5316\u8f93\u51fa\u4e3a\u767e\u5206\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u663e\u793a\u767e\u5206\u6570\u3002\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\u6216\u8005f-string\uff08Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u90fd\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c<code>&quot;{:.2%}&quot;.format(0.123)<\/code> \u6216\u8005 <code>f&quot;{0.123:.2%}&quot;<\/code> \u90fd\u4f1a\u5c060.123\u683c\u5f0f\u5316\u4e3a\u201c12.30%\u201d\uff0c\u5176\u4e2d\u201c.2\u201d\u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u663e\u793a\u767e\u5206\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0cPython\u7684<code>matplotlib<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86\u663e\u793a\u767e\u5206\u6570\u7684\u529f\u80fd\u3002\u5f53\u7ed8\u5236\u56fe\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PercentFormatter<\/code>\u7c7b\u6765\u5c06\u5750\u6807\u8f74\u7684\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u767e\u5206\u6570\u683c\u5f0f\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u5f97\u6570\u636e\u5c55\u793a\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Pandas\u4e2d\u663e\u793a\u767e\u5206\u6570\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pandas\u5904\u7406\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>style.format<\/code>\u65b9\u6cd5\u6765\u663e\u793aDataFrame\u4e2d\u7684\u767e\u5206\u6570\u3002\u4f8b\u5982\uff0c<code>df.style.format(&quot;{:.2%}&quot;)<\/code>\u4f1a\u5c06DataFrame\u4e2d\u6240\u6709\u7684\u6570\u503c\u683c\u5f0f\u5316\u4e3a\u767e\u5206\u6570\u663e\u793a\u3002\u8fd9\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u548c\u62a5\u544a\u5c24\u4e3a\u91cd\u8981\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u663e\u793a\u767e\u5206\u6570\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f7f\u7528f-string\u3001\u501f\u52a9\u683c\u5f0f\u5316\u51fd\u6570\u3002\u5176\u4e2d\uff0c\u4f7f\u7528f- [&hellip;]","protected":false},"author":3,"featured_media":1134005,"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\/1133995"}],"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=1133995"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133995\/revisions"}],"predecessor-version":[{"id":1134008,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133995\/revisions\/1134008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134005"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}