{"id":1144591,"date":"2025-01-08T23:01:06","date_gmt":"2025-01-08T15:01:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144591.html"},"modified":"2025-01-08T23:01:08","modified_gmt":"2025-01-08T15:01:08","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e5%80%bc%e8%bd%ac%e6%8d%a2%e6%88%90%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144591.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u6570\u503c\u8f6c\u6362\u6210\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181610\/167e7273-8561-4bea-8f4d-e9ad77254f95.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u6570\u503c\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06\u6570\u503c\u8f6c\u6362\u6210\u5b57\u7b26\u4e32<\/strong>\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>str()<\/code>\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3001\u4f7f\u7528f-string<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u90fd\u6709\u5176\u4f18\u70b9\uff0c\u5176\u4e2d\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u662f\u6700\u57fa\u672c\u548c\u901a\u7528\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>str()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570<code>str()<\/code>\uff0c\u53ef\u4ee5\u5c06\u6570\u503c\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6570\u503c\u53d8\u91cf<code>num<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>str(num)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>str_num = str(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u9ad8\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u57fa\u672c\u8f6c\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u767e\u5206\u53f7<code>%<\/code>\u3001<code>str.format()<\/code>\u65b9\u6cd5\u4ee5\u53ca\u6700\u65b0\u7684f-string\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>1. \u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316\u662fPython\u4e2d\u8f83\u65e9\u63d0\u4f9b\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u867d\u7136\u73b0\u5728\u5df2\u7ecf\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\uff0c\u4f46\u5728\u8001\u4ee3\u7801\u4e2d\u4ecd\u7136\u5f88\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>str_num = &quot;%d&quot; % num<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. <code>str.format()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.format()<\/code>\u65b9\u6cd5\u662fPython 3\u5f15\u5165\u7684\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u683c\u5f0f\u5316\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>str_num = &quot;{}&quot;.format(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><h3>3. f-string<\/h3>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u8bed\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>str_num = f&quot;{num}&quot;<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u8bed\u6cd5\u7b80\u6d01\uff0c\u6027\u80fd\u8f83\u9ad8\uff0c\u63a8\u8350\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3001\u6dfb\u52a0\u5343\u5206\u4f4d\u5206\u9694\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6216f-string\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.456<\/p>\n<p>str_num = &quot;{:.2f}&quot;.format(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123.46&#39;<\/p>\n<p>str_num = f&quot;{num:.2f}&quot;<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6dfb\u52a0\u5343\u5206\u4f4d\u5206\u9694\u7b26<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6216f-string\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 1234567.89<\/p>\n<p>str_num = &quot;{:,}&quot;.format(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;1,234,567.89&#39;<\/p>\n<p>str_num = f&quot;{num:,}&quot;<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;1,234,567.89&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u5c06\u6570\u503c\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>str()<\/code>\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3001\u4f7f\u7528f-string<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u90fd\u6709\u5176\u4f18\u70b9\uff0c\u5176\u4e2d\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u662f\u6700\u57fa\u672c\u548c\u901a\u7528\u7684\u65b9\u5f0f\u3002\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u4ee3\u7801\u98ce\u683c\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>str()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>str()<\/code>\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5c06\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u590d\u6570\u7b49\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>num = 123<\/p>\n<p>str_num = str(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>num = 123.456<\/p>\n<p>str_num = str(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123.456&#39;<\/p>\n<h2><strong>\u5c06\u590d\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>num = 1 + 2j<\/p>\n<p>str_num = str(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;(1+2j)&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u7f3a\u70b9\u5206\u6790<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u660e\u4e86<\/strong>\uff1a\u4ee3\u7801\u77ed\u5c0f\u7cbe\u608d\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<li><strong>\u901a\u7528\u6027\u5f3a<\/strong>\uff1a\u9002\u7528\u4e8e\u5404\u79cd\u6570\u503c\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u5355\u4e00<\/strong>\uff1a\u4ec5\u80fd\u8fdb\u884c\u57fa\u672c\u7684\u6570\u503c\u8f6c\u6362\uff0c\u65e0\u6cd5\u8fdb\u884c\u590d\u6742\u7684\u683c\u5f0f\u5316\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u5305\u62ec<strong>\u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316\u3001<code>str.format()<\/code>\u65b9\u6cd5\u3001f-string<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316\u662fPython\u4e2d\u8f83\u65e9\u63d0\u4f9b\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>num = 123<\/p>\n<p>str_num = &quot;%d&quot; % num<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>num = 123.456<\/p>\n<p>str_num = &quot;%.2f&quot; % num<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u4f18\u7f3a\u70b9\u5206\u6790<\/h5>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u76f4\u63a5<\/strong>\uff1a\u9002\u5408\u8fdb\u884c\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u53ef\u8bfb\u6027\u8f83\u5dee<\/strong>\uff1a\u5728\u5904\u7406\u590d\u6742\u683c\u5f0f\u5316\u65f6\uff0c\u4ee3\u7801\u4e0d\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n<li><strong>\u5df2\u8fc7\u65f6<\/strong>\uff1a\u5728Python 3\u4e2d\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><h4>2. <code>str.format()<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>str.format()<\/code>\u65b9\u6cd5\u662fPython 3\u5f15\u5165\u7684\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u683c\u5f0f\u5316\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>num = 123<\/p>\n<p>str_num = &quot;{}&quot;.format(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>num = 123.456<\/p>\n<p>str_num = &quot;{:.2f}&quot;.format(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u4f18\u7f3a\u70b9\u5206\u6790<\/h5>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027\u597d<\/strong>\uff1a\u4ee3\u7801\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bed\u6cd5\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u76f8\u5bf9\u4e8ef-string\uff0c\u8bed\u6cd5\u7a0d\u663e\u7e41\u7410\u3002<\/li>\n<\/ul>\n<p><h4>3. f-string<\/h4>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u8bed\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>num = 123<\/p>\n<p>str_num = f&quot;{num}&quot;<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>num = 123.456<\/p>\n<p>str_num = f&quot;{num:.2f}&quot;<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u4f18\u7f3a\u70b9\u5206\u6790<\/h5>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bed\u6cd5\u7b80\u6d01<\/strong>\uff1a\u6613\u4e8e\u7f16\u5199\u548c\u9605\u8bfb\u3002<\/li>\n<li><strong>\u6027\u80fd\u8f83\u9ad8<\/strong>\uff1a\u76f8\u5bf9\u4e8e\u5176\u4ed6\u65b9\u6cd5\uff0cf-string\u5728\u6027\u80fd\u4e0a\u6709\u4e00\u5b9a\u4f18\u52bf\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7248\u672c\u9650\u5236<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8ePython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3001\u6dfb\u52a0\u5343\u5206\u4f4d\u5206\u9694\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6216f-string\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528str.format()\u65b9\u6cd5\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d<\/p>\n<p>num = 123.456<\/p>\n<p>str_num = &quot;{:.2f}&quot;.format(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123.46&#39;<\/p>\n<h2><strong>\u4f7f\u7528f-string\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d<\/strong><\/h2>\n<p>str_num = f&quot;{num:.2f}&quot;<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6dfb\u52a0\u5343\u5206\u4f4d\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6216f-string\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528str.format()\u65b9\u6cd5\u6dfb\u52a0\u5343\u5206\u4f4d\u5206\u9694\u7b26<\/p>\n<p>num = 1234567.89<\/p>\n<p>str_num = &quot;{:,}&quot;.format(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;1,234,567.89&#39;<\/p>\n<h2><strong>\u4f7f\u7528f-string\u6dfb\u52a0\u5343\u5206\u4f4d\u5206\u9694\u7b26<\/strong><\/h2>\n<p>str_num = f&quot;{num:,}&quot;<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;1,234,567.89&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u7279\u6b8a\u6570\u503c\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5e38\u89c1\u7684\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff0cPython\u8fd8\u652f\u6301\u5176\u4ed6\u4e00\u4e9b\u6570\u503c\u7c7b\u578b\uff0c\u4f8b\u5982\u590d\u6570\u3001\u5e03\u5c14\u503c\u7b49\u3002\u8fd9\u4e9b\u6570\u503c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1. \u8f6c\u6362\u590d\u6570<\/h4>\n<\/p>\n<p><p>\u590d\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u79cd\u6570\u503c\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>str()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">num = 1 + 2j<\/p>\n<p>str_num = str(num)<\/p>\n<p>print(str_num)  # \u8f93\u51fa: &#39;(1+2j)&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8f6c\u6362\u5e03\u5c14\u503c<\/h4>\n<\/p>\n<p><p>\u5e03\u5c14\u503c\u5728Python\u4e2d\u4e5f\u662f\u4e00\u79cd\u6570\u503c\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>str()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">flag = True<\/p>\n<p>str_flag = str(flag)<\/p>\n<p>print(str_flag)  # \u8f93\u51fa: &#39;True&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u6218\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u9700\u6c42\u975e\u5e38\u5e38\u89c1\u3002\u4e0b\u9762\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u65e5\u5fd7\u8bb0\u5f55\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u6570\u503c\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<h2><strong>\u6a21\u62df\u6570\u636e<\/strong><\/h2>\n<p>user_id = 123<\/p>\n<p>transaction_amount = 456.78<\/p>\n<h2><strong>\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logging.info(f&quot;User ID: {user_id}, Transaction Amount: {transaction_amount:.2f}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u5c55\u793a<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5c55\u793a\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5c06\u6570\u503c\u6570\u636e\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u5e76\u5c55\u793a\u7ed9\u7528\u6237\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u4ee3\u7801<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6a21\u62df\u6570\u636e<\/p>\n<p>sales = 1234567.89<\/p>\n<h2><strong>\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5e26\u5343\u5206\u4f4d\u5206\u9694\u7b26\u7684\u5b57\u7b26\u4e32\u5e76\u5c55\u793a<\/strong><\/h2>\n<p>print(f&quot;Total Sales: ${sales:,.2f}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>str()<\/code>\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3001\u4f7f\u7528f-string<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u6700\u4e3a\u57fa\u7840\u548c\u901a\u7528<\/strong>\uff0c\u800c<strong>f-string\u7531\u4e8e\u5176\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u9ad8\u6548\u7684\u6027\u80fd\uff0c\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u66f4\u4e3a\u63a8\u8350<\/strong>\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6613\u8bfb\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>str()<\/code>\u51fd\u6570\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>number = 123<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>string_number = str(number)<\/code>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\uff0c<code>string_number<\/code>\u7684\u503c\u5c06\u662f<code>&#39;123&#39;<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u65b9\u6cd5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u65b9\u6cd5\u6765\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528<code>f-string<\/code>\uff08\u4f8b\u5982\uff1a<code>f&quot;{number}&quot;<\/code>\uff09\uff0c\u6216\u8005\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\uff08\u4f8b\u5982\uff1a<code>&quot;{}&quot;.format(number)<\/code>\uff09\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u8f6c\u6362\u6570\u5b57\uff0c\u8fd8\u80fd\u7075\u6d3b\u5730\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u5305\u62ec\u6307\u5b9a\u5c0f\u6570\u4f4d\u6570\u7b49\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8f6c\u6362\u6d6e\u70b9\u6570\u4e3a\u5b57\u7b26\u4e32\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7cbe\u5ea6\u95ee\u9898\u3002\u4f8b\u5982\uff0c<code>number = 0.1 + 0.2<\/code>\uff0c\u76f4\u63a5\u8f6c\u6362\u53ef\u80fd\u4f1a\u5f97\u5230<code>&#39;0.30000000000000004&#39;<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5982<code>&quot;{:.2f}&quot;.format(number)<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u63a7\u5236\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\uff0c\u7ed3\u679c\u4f1a\u662f<code>&#39;0.30&#39;<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u6570\u503c\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570str()\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3001\u4f7f\u7528f- [&hellip;]","protected":false},"author":3,"featured_media":1144596,"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\/1144591"}],"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=1144591"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144591\/revisions"}],"predecessor-version":[{"id":1144598,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144591\/revisions\/1144598"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144596"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}