{"id":1155332,"date":"2025-01-13T18:02:09","date_gmt":"2025-01-13T10:02:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155332.html"},"modified":"2025-01-13T18:02:12","modified_gmt":"2025-01-13T10:02:12","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%be%93%e5%87%ba%e5%a1%ab%e5%85%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1155332.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u8f93\u51fa\u586b\u5145"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25193954\/25bab38f-12d9-426e-93ef-6ddabe24912d.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u8f93\u51fa\u586b\u5145\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u8f93\u51fa\u586b\u5145\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u5b57\u7b26\u4e32\u7684<code>ljust()<\/code>\u3001<code>rjust()<\/code>\u548c<code>center()<\/code>\u65b9\u6cd5\uff0c<code>format()<\/code>\u65b9\u6cd5\uff0cf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff09\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c<code>format()<\/code>\u65b9\u6cd5\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u4f60\u5b9a\u4e49\u66f4\u590d\u6742\u7684\u586b\u5145\u548c\u683c\u5f0f\u89c4\u5219\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello&quot;<\/p>\n<p>filled_text = &quot;{:&lt;10}&quot;.format(text)<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;Hello     &#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>{:&lt;10}<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u586b\u5145\u81f310\u4e2a\u5b57\u7b26\u5bbd\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u586b\u5145\u65b9\u6848\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528f-string\u548c\u5176\u4ed6\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001ljust()\u3001rjust()\u3001center() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1. ljust()<\/h4>\n<\/p>\n<p><p><code>ljust()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5de6\u5bf9\u9f50\uff0c\u5e76\u4f7f\u7528\u6307\u5b9a\u7684\u5b57\u7b26\u586b\u5145\u53f3\u8fb9\u7684\u7a7a\u767d\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello&quot;<\/p>\n<p>filled_text = text.ljust(10, &#39;-&#39;)<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;Hello-----&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. rjust()<\/h4>\n<\/p>\n<p><p><code>rjust()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u53f3\u5bf9\u9f50\uff0c\u5e76\u4f7f\u7528\u6307\u5b9a\u7684\u5b57\u7b26\u586b\u5145\u5de6\u8fb9\u7684\u7a7a\u767d\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello&quot;<\/p>\n<p>filled_text = text.rjust(10, &#39;-&#39;)<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;-----Hello&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. center()<\/h4>\n<\/p>\n<p><p><code>center()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5c45\u4e2d\u5bf9\u9f50\uff0c\u5e76\u4f7f\u7528\u6307\u5b9a\u7684\u5b57\u7b26\u586b\u5145\u4e24\u8fb9\u7684\u7a7a\u767d\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello&quot;<\/p>\n<p>filled_text = text.center(10, &#39;-&#39;)<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;--Hello---&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001format() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>format()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u586b\u5145\u3001\u5bf9\u9f50\u548c\u5bbd\u5ea6\u7b49\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello&quot;<\/p>\n<p>filled_text = &quot;{:&lt;10}&quot;.format(text)<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;Hello     &#39;<\/p>\n<h2><strong>\u53f3\u5bf9\u9f50<\/strong><\/h2>\n<p>filled_text = &quot;{:&gt;10}&quot;.format(text)<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;     Hello&#39;<\/p>\n<h2><strong>\u5c45\u4e2d\u5bf9\u9f50<\/strong><\/h2>\n<p>filled_text = &quot;{:^10}&quot;.format(text)<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;  Hello   &#39;<\/p>\n<h2><strong>\u4f7f\u7528\u7279\u5b9a\u5b57\u7b26\u586b\u5145<\/strong><\/h2>\n<p>filled_text = &quot;{:-&lt;10}&quot;.format(text)<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;Hello-----&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff09<\/h3>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u5e76\u4e14\u652f\u6301\u586b\u5145\u3001\u5bf9\u9f50\u548c\u5bbd\u5ea6\u7b49\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello&quot;<\/p>\n<p>filled_text = f&quot;{text:&lt;10}&quot;<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;Hello     &#39;<\/p>\n<h2><strong>\u53f3\u5bf9\u9f50<\/strong><\/h2>\n<p>filled_text = f&quot;{text:&gt;10}&quot;<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;     Hello&#39;<\/p>\n<h2><strong>\u5c45\u4e2d\u5bf9\u9f50<\/strong><\/h2>\n<p>filled_text = f&quot;{text:^10}&quot;<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;  Hello   &#39;<\/p>\n<h2><strong>\u4f7f\u7528\u7279\u5b9a\u5b57\u7b26\u586b\u5145<\/strong><\/h2>\n<p>filled_text = f&quot;{text:-&lt;10}&quot;<\/p>\n<p>print(filled_text)  # \u8f93\u51fa\uff1a&#39;Hello-----&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u8f93\u51fa\u586b\u5145\uff0c\u5982\u4f7f\u7528<code>zfill()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u586b\u5145\u6570\u5b57\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1. zfill()<\/h4>\n<\/p>\n<p><p><code>zfill()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u524d\u9762\u586b\u5145\u96f6\uff0c\u4ee5\u8fbe\u5230\u6307\u5b9a\u7684\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = &quot;42&quot;<\/p>\n<p>filled_number = number.zfill(5)<\/p>\n<p>print(filled_number)  # \u8f93\u51fa\uff1a&#39;00042&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b9e\u8df5\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1. \u8868\u683c\u5bf9\u9f50<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u8868\u683c\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u6bcf\u4e00\u5217\u7684\u5185\u5bb9\u5bf9\u9f50\uff0c\u4ee5\u4fbf\u8868\u683c\u66f4\u6613\u4e8e\u9605\u8bfb\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;},<\/p>\n<p>]<\/p>\n<p>print(&quot;{:&lt;10} {:&lt;5} {:&lt;15}&quot;.format(&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;))<\/p>\n<p>for row in data:<\/p>\n<p>    print(&quot;{:&lt;10} {:&lt;5} {:&lt;15}&quot;.format(row[&quot;name&quot;], row[&quot;age&quot;], row[&quot;city&quot;]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u5b57\u586b\u5145<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u5b57\u65f6\uff0c\u6709\u65f6\u9700\u8981\u5c06\u6570\u5b57\u586b\u5145\u4e3a\u56fa\u5b9a\u5bbd\u5ea6\uff0c\u4ee5\u4fbf\u4e8e\u6392\u5e8f\u6216\u5bf9\u9f50\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 12, 123, 1234]<\/p>\n<p>for number in numbers:<\/p>\n<p>    print(&quot;{:04}&quot;.format(number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8f93\u51fa\u586b\u5145\uff0c\u5305\u62ec<code>ljust()<\/code>\u3001<code>rjust()<\/code>\u3001<code>center()<\/code>\u3001<code>format()<\/code>\u3001f-string\u548c<code>zfill()<\/code>\u7b49\u3002<strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/strong> \u901a\u8fc7\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6587\u672c\u5bf9\u9f50\u3001\u6570\u5b57\u586b\u5145\u7b49\u9700\u6c42\uff0c\u4f7f\u8f93\u51fa\u66f4\u52a0\u7f8e\u89c2\u548c\u89c4\u8303\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u586b\u5145\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.ljust()<\/code>, <code>str.rjust()<\/code>, \u548c <code>str.center()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u586b\u5145\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u60a8\u6307\u5b9a\u586b\u5145\u5b57\u7b26\u548c\u586b\u5145\u7684\u603b\u957f\u5ea6\u3002\u4f8b\u5982\uff0c<code>my_string.ljust(10, &#39; &#39;)<\/code>\u5c06\u5b57\u7b26\u4e32<code>my_string<\/code>\u5de6\u5bf9\u9f50\u5e76\u5728\u53f3\u4fa7\u586b\u5145\u7a7a\u683c\uff0c\u76f4\u5230\u603b\u957f\u5ea6\u8fbe\u523010\u4e2a\u5b57\u7b26\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u586b\u5145\u5b57\u7b26\uff1f<\/strong><br \/>\u5e38\u7528\u7684\u586b\u5145\u5b57\u7b26\u5305\u62ec\u7a7a\u683c\u3001\u96f6\uff08&#39;0&#39;\uff09\u3001\u661f\u53f7\uff08&#39;*&#39;\uff09\u7b49\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u586b\u5145\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u8981\u4ee5\u96f6\u586b\u5145\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.zfill(width)<\/code>\uff0c\u5b83\u4f1a\u5728\u6570\u5b57\u524d\u9762\u6dfb\u52a0\u96f6\uff0c\u4f7f\u5176\u8fbe\u5230\u6307\u5b9a\u7684\u5bbd\u5ea6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6570\u5b57\u586b\u5145\u4ee5\u786e\u4fdd\u8f93\u51fa\u683c\u5f0f\u4e00\u81f4\uff1f<\/strong><br \/>\u5bf9\u4e8e\u6570\u5b57\u586b\u5145\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u6216<code>format()<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u683c\u5f0f\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u5bbd\u5ea6\u548c\u586b\u5145\u5b57\u7b26\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u81f4\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>f&quot;{number:05}&quot;<\/code>\u53ef\u4ee5\u5c06\u6570\u5b57\u586b\u5145\u4e3a5\u4f4d\uff0c\u4e0d\u8db3\u7684\u90e8\u5206\u7528\u96f6\u586b\u5145\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u751f\u6210\u62a5\u544a\u6216\u8f93\u51fa\u5bf9\u9f50\u7684\u8868\u683c\u65f6\u5c24\u5176\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u8f93\u51fa\u586b\u5145\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u5b57\u7b26\u4e32\u7684ljust()\u3001rjust()\u548ccenter()\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1155335,"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\/1155332"}],"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=1155332"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155332\/revisions"}],"predecessor-version":[{"id":1155336,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155332\/revisions\/1155336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155335"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}