{"id":1011732,"date":"2024-12-27T11:32:37","date_gmt":"2024-12-27T03:32:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011732.html"},"modified":"2024-12-27T11:32:39","modified_gmt":"2024-12-27T03:32:39","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e6%89%93%e5%8d%b0%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011732.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u6253\u5370\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085741\/fb7c3654-59ff-4d5f-b8ce-dda7944237e4.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u6253\u5370\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5b9a\u4e49\u6253\u5370\u683c\u5f0f\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001\u4f7f\u7528str.format()\u65b9\u6cd5\u3001\u4f7f\u7528f-string\u683c\u5f0f\u5316\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0cf-string\u683c\u5f0f\u5316\u662fPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u6700\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u76f4\u89c2\u3001\u6613\u4e8e\u9605\u8bfb\u548c\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u89e3\u91caf-string\u683c\u5f0f\u5316\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u7b80\u5355\u56de\u987e\u4e00\u4e0b\u5176\u4ed6\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u8f83\u4f20\u7edf\uff0c\u4f7f\u7528\u767e\u5206\u53f7<code>%<\/code>\u4f5c\u4e3a\u5360\u4f4d\u7b26\u3002\u4f8b\u5982\uff0c<code>&quot;%s is %d years old&quot; % (&quot;Alice&quot;, 30)<\/code>\u5c06\u8f93\u51fa<code>Alice is 30 years old<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u7f3a\u70b9\u662f\u5bf9\u4e8e\u590d\u6742\u7684\u683c\u5f0f\u5316\u64cd\u4f5c\u652f\u6301\u4e0d\u4f73\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>str.format()\u65b9\u6cd5<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u901a\u8fc7<code>{}<\/code>\u5360\u4f4d\u7b26\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c<code>&quot;{} is {} years old&quot;.format(&quot;Alice&quot;, 30)<\/code>\u540c\u6837\u4f1a\u8f93\u51fa<code>Alice is 30 years old<\/code>\u3002\u8be5\u65b9\u6cd5\u63d0\u4f9b\u4e86\u5bf9\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bbaf-string\u683c\u5f0f\u5316\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001F-STRING\u683c\u5f0f\u5316<\/h3>\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\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b0\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a<code>f<\/code>\u6216<code>F<\/code>\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>f-string\u5141\u8bb8\u6211\u4eec\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\u3002\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>message = f&quot;{name} is {age} years old&quot;<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>Alice is 30 years old<\/code>\u3002\u5728\u5927\u62ec\u53f7\u5185\u53ef\u4ee5\u76f4\u63a5\u5199\u53d8\u91cf\u540d\uff0c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u683c\u5f0f\u5316\u6570\u5b57<\/h4>\n<\/p>\n<p><p>f-string\u652f\u6301\u5bf9\u6570\u5b57\u8fdb\u884c\u683c\u5f0f\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 123.456789<\/p>\n<p>formatted_value = f&quot;{value:.2f}&quot;  # \u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<\/p>\n<p>print(formatted_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>123.46<\/code>\u3002<code>.2f<\/code>\u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5b57\u7b26\u4e32\u5bf9\u9f50<\/h4>\n<\/p>\n<p><p>f-string\u4e5f\u652f\u6301\u5b57\u7b26\u4e32\u5bf9\u9f50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Bob&quot;<\/p>\n<p>print(f&quot;{name:&lt;10}&quot;)  # \u5de6\u5bf9\u9f50\uff0c\u536010\u4e2a\u5b57\u7b26\u5bbd\u5ea6<\/p>\n<p>print(f&quot;{name:&gt;10}&quot;)  # \u53f3\u5bf9\u9f50\uff0c\u536010\u4e2a\u5b57\u7b26\u5bbd\u5ea6<\/p>\n<p>print(f&quot;{name:^10}&quot;)  # \u5c45\u4e2d\u5bf9\u9f50\uff0c\u536010\u4e2a\u5b57\u7b26\u5bbd\u5ea6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Bob       <\/p>\n<p>       Bob<\/p>\n<p>   Bob    <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u4f7f\u7528<code>%<\/code>\u7b26\u53f7\u6765\u8fdb\u884c\u683c\u5f0f\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Charlie&quot;<\/p>\n<p>age = 25<\/p>\n<p>message = &quot;%s is %d years old&quot; % (name, age)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>Charlie is 25 years old<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u683c\u5f0f\u5316\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u683c\u5f0f\u7b26\u6765\u683c\u5f0f\u5316\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 123.456789<\/p>\n<p>formatted_value = &quot;%.2f&quot; % value  # \u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<\/p>\n<p>print(formatted_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>123.46<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5b57\u7b26\u4e32\u5bf9\u9f50<\/h4>\n<\/p>\n<p><p>\u5728\u767e\u5206\u53f7\u683c\u5f0f\u5316\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5bbd\u5ea6\u8fdb\u884c\u5b57\u7b26\u4e32\u5bf9\u9f50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;David&quot;<\/p>\n<p>print(&quot;%-10s&quot; % name)  # \u5de6\u5bf9\u9f50<\/p>\n<p>print(&quot;%10s&quot; % name)   # \u53f3\u5bf9\u9f50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>David     <\/p>\n<p>     David<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001STR.FORMAT()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>str.format()<\/code>\u65b9\u6cd5\u4f7f\u7528\u5927\u62ec\u53f7<code>{}<\/code>\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Eve&quot;<\/p>\n<p>age = 28<\/p>\n<p>message = &quot;{} is {} years old&quot;.format(name, age)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>Eve is 28 years old<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u683c\u5f0f\u5316\u6570\u5b57<\/h4>\n<\/p>\n<p><p><code>str.format()<\/code>\u4e5f\u652f\u6301\u6570\u5b57\u683c\u5f0f\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 123.456789<\/p>\n<p>formatted_value = &quot;{:.2f}&quot;.format(value)  # \u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<\/p>\n<p>print(formatted_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>123.46<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5b57\u7b26\u4e32\u5bf9\u9f50<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>str.format()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8fdb\u884c\u5b57\u7b26\u4e32\u5bf9\u9f50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Frank&quot;<\/p>\n<p>print(&quot;{:&lt;10}&quot;.format(name))  # \u5de6\u5bf9\u9f50<\/p>\n<p>print(&quot;{:&gt;10}&quot;.format(name))  # \u53f3\u5bf9\u9f50<\/p>\n<p>print(&quot;{:^10}&quot;.format(name))  # \u5c45\u4e2d\u5bf9\u9f50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Frank     <\/p>\n<p>     Frank<\/p>\n<p>  Frank   <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6253\u5370\u683c\u5f0f\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002<strong>\u5728\u73b0\u4ee3Python\u4ee3\u7801\u4e2d\uff0cf-string\u683c\u5f0f\u5316\u56e0\u5176\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u800c\u88ab\u5e7f\u6cdb\u63a8\u8350\u4f7f\u7528<\/strong>\u3002\u767e\u5206\u53f7\u683c\u5f0f\u5316\u548c<code>str.format()<\/code>\u65b9\u6cd5\u867d\u7136\u4ecd\u7136\u53ef\u7528\uff0c\u4f46\u5728\u65b0\u4ee3\u7801\u4e2d\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u8f93\u51fa\u7684\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u6765\u5b9a\u4e49\u6253\u5370\u683c\u5f0f\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ecf-\u5b57\u7b26\u4e32\u3001<code>str.format()<\/code>\u548c\u767e\u5206\u53f7(<code>%<\/code>)\u683c\u5f0f\u5316\u3002f-\u5b57\u7b26\u4e32\u662fPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u5f15\u5165\u7684\uff0c\u8bed\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u4e8e\u590d\u6742\u7684\u683c\u5f0f\u5316\u9700\u6c42\u3002\u4f8b\u5982\uff1a<code>print(f&quot;Hello, {name}. You are {age} years old.&quot;)<\/code>\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u5f0f\u6765\u683c\u5f0f\u5316\u6570\u5b57\u548c\u5c0f\u6570\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u683c\u5f0f\u5316\u6570\u5b57\u548c\u5c0f\u6570\uff0c\u4f8b\u5982\u901a\u8fc7f-\u5b57\u7b26\u4e32\u53ef\u4ee5\u76f4\u63a5\u5728\u82b1\u62ec\u53f7\u5185\u6307\u5b9a\u683c\u5f0f\uff0c\u6bd4\u5982<code>print(f&quot;{value:.2f}&quot;)<\/code>\uff0c\u8fd9\u91cc\u7684<code>.2f<\/code>\u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u6b64\u5916\uff0c<code>str.format()<\/code>\u65b9\u6cd5\u4e5f\u652f\u6301\u7c7b\u4f3c\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5982<code>print(&quot;Value: {:.2f}&quot;.format(value))<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6253\u5370\u65f6\u6dfb\u52a0\u524d\u5bfc\u96f6\u6216\u586b\u5145\u5b57\u7b26\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u8bf4\u660e\u7b26\u6765\u63a7\u5236\u6253\u5370\u65f6\u7684\u524d\u5bfc\u96f6\u6216\u586b\u5145\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5728f-\u5b57\u7b26\u4e32\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>{number:05}<\/code>\u6765\u6307\u5b9a\u8f93\u51fa\u7684\u6570\u5b57\u5bbd\u5ea6\u4e3a5\u4f4d\uff0c\u4e0d\u8db3\u90e8\u5206\u7528\u96f6\u586b\u5145\u3002\u5982\u679c\u4f7f\u7528<code>str.format()<\/code>\uff0c\u540c\u6837\u53ef\u4ee5\u5199\u6210<code>&quot;{:05}&quot;.format(number)<\/code>\u3002\u8fd9\u4e9b\u65b9\u5f0f\u90fd\u80fd\u5e2e\u52a9\u4f60\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5b9a\u4e49\u6253\u5370\u683c\u5f0f\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001\u4f7f\u7528str.format()\u65b9\u6cd5\u3001\u4f7f\u7528f-stri [&hellip;]","protected":false},"author":3,"featured_media":1011737,"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\/1011732"}],"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=1011732"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011732\/revisions"}],"predecessor-version":[{"id":1011739,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011732\/revisions\/1011739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011737"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}