{"id":1006880,"date":"2024-12-27T10:48:24","date_gmt":"2024-12-27T02:48:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1006880.html"},"modified":"2024-12-27T10:48:27","modified_gmt":"2024-12-27T02:48:27","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8format","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1006880.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528format"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082824\/e2938678-031b-4cb4-8f3b-b5c49d8a32c6.webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528format\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002<strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u63d2\u5165\u53d8\u91cf\u503c\u3001\u63a7\u5236\u8f93\u51fa\u683c\u5f0f<\/strong>\u662f<code>format<\/code>\u65b9\u6cd5\u7684\u6838\u5fc3\u7528\u9014\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u7528\u9014\uff0c\u5e76\u63d0\u4f9b\u4f7f\u7528\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<code>FORMAT<\/code>\u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>format<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u503c\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u901a\u8fc7<code>{}<\/code>\u5360\u4f4d\u7b26\u6765\u6307\u5b9a\u63d2\u5165\u4f4d\u7f6e\uff0c\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5\u586b\u5145\u8fd9\u4e9b\u5360\u4f4d\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>formatted_string = &quot;My name is {} and I am {} years old.&quot;.format(name, age)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>{}<\/code>\u5360\u4f4d\u7b26\u88ab<code>name<\/code>\u548c<code>age<\/code>\u53d8\u91cf\u7684\u503c\u6240\u66ff\u6362\uff0c\u751f\u6210\u7684\u5b57\u7b26\u4e32\u88ab\u8d4b\u7ed9<code>formatted_string<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6307\u5b9a\u4f4d\u7f6e<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u9ed8\u8ba4\u7684\u4ece\u5de6\u5230\u53f3\u586b\u5145\u5360\u4f4d\u7b26\u7684\u65b9\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4f4d\u7f6e\u6765\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u63d2\u503c\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_string = &quot;My name is {1} and I am {0} years old.&quot;.format(age, name)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u7d22\u5f15\u6307\u5b9a\u4e86\u63d2\u5165\u987a\u5e8f\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u4e0d\u540c\u4e8e\u53d8\u91cf\u987a\u5e8f\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u547d\u540d\u5360\u4f4d\u7b26<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u547d\u540d\u5360\u4f4d\u7b26\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u5e76\u4e14\u5728\u63d2\u5165\u591a\u4e2a\u53d8\u91cf\u65f6\u907f\u514d\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_string = &quot;My name is {name} and I am {age} years old.&quot;.format(name=name, age=age)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u591a\u4e2a\u53d8\u91cf\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u683c\u5f0f\u5316\u6570\u5b57<\/p>\n<\/p>\n<p><p><code>format<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u6570\u5b57\uff0c\u5305\u62ec\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u3001\u8bbe\u7f6e\u5343\u4f4d\u5206\u9694\u7b26\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5c0f\u6570\u4f4d\u6570<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">pi = 3.141592653589793<\/p>\n<p>formatted_string = &quot;Pi is approximately {:.2f}&quot;.format(pi)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>:.2f<\/code>\u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\uff0c\u751f\u6210\u7684\u5b57\u7b26\u4e32\u5c06\u663e\u793a<code>Pi is approximately 3.14<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5343\u4f4d\u5206\u9694\u7b26<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">number = 1000000<\/p>\n<p>formatted_string = &quot;The number is {:,}&quot;.format(number)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u5c06\u5305\u542b\u5343\u4f4d\u5206\u9694\u7b26\uff0c\u5373<code>The number is 1,000,000<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5bf9\u9f50\u6587\u672c<\/p>\n<\/p>\n<p><p><code>format<\/code>\u65b9\u6cd5\u652f\u6301\u6587\u672c\u5bf9\u9f50\uff0c\u901a\u8fc7\u5728\u5360\u4f4d\u7b26\u4e2d\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\uff0c\u53ef\u4ee5\u63a7\u5236\u8f93\u51fa\u6587\u672c\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5de6\u5bf9\u9f50<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">formatted_string = &quot;{:&lt;10}&quot;.format(&quot;left&quot;)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>left      <\/code>\uff0c\u6587\u672c\u88ab\u5de6\u5bf9\u9f50\uff0c\u53f3\u4fa7\u586b\u5145\u7a7a\u683c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u53f3\u5bf9\u9f50<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">formatted_string = &quot;{:&gt;10}&quot;.format(&quot;right&quot;)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>     right<\/code>\uff0c\u6587\u672c\u88ab\u53f3\u5bf9\u9f50\uff0c\u5de6\u4fa7\u586b\u5145\u7a7a\u683c\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5c45\u4e2d\u5bf9\u9f50<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">formatted_string = &quot;{:^10}&quot;.format(&quot;center&quot;)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code> center <\/code>\uff0c\u6587\u672c\u572810\u4e2a\u5b57\u7b26\u5bbd\u5ea6\u5185\u5c45\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u586b\u5145\u5b57\u7b26<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u7a7a\u683c\uff0c<code>format<\/code>\u65b9\u6cd5\u8fd8\u5141\u8bb8\u6307\u5b9a\u5176\u4ed6\u5b57\u7b26\u6765\u586b\u5145\u5bf9\u9f50\u540e\u7684\u7a7a\u767d\u533a\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_string = &quot;{:*^10}&quot;.format(&quot;center&quot;)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u4e3a<code>&lt;strong&gt;center&lt;\/strong&gt;<\/code>\uff0c\u4f7f\u7528<code>*<\/code>\u4f5c\u4e3a\u586b\u5145\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u683c\u5f0f\u5316\u767e\u5206\u6bd4<\/p>\n<\/p>\n<p><p><code>format<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u767e\u5206\u6bd4\u5f62\u5f0f\uff0c\u65b9\u4fbf\u5730\u5c55\u793a\u767e\u5206\u6bd4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">percentage = 0.75<\/p>\n<p>formatted_string = &quot;The success rate is {:.0%}&quot;.format(percentage)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>The success rate is 75%<\/code>\uff0c\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u767e\u5206\u6bd4\u8868\u793a\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>format<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\uff0c\u901a\u8fc7\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>formatted_string = &quot;Current time is: {:%Y-%m-%d %H:%M:%S}&quot;.format(now)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a<code>Year-Month-Day Hour:Minute:Second<\/code>\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u7c7b<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u683c\u5f0f\u5316\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u7c7b\u5b9e\u73b0<code>__format__<\/code>\u65b9\u6cd5\uff0c\u6765\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __format__(self, format_spec):<\/p>\n<p>        if format_spec == &#39;name&#39;:<\/p>\n<p>            return self.name<\/p>\n<p>        elif format_spec == &#39;age&#39;:<\/p>\n<p>            return str(self.age)<\/p>\n<p>        return f&quot;{self.name}, {self.age}&quot;<\/p>\n<p>person = Person(&quot;Alice&quot;, 30)<\/p>\n<p>formatted_string = &quot;Name: {:name}, Age: {:age}&quot;.format(person, person)<\/p>\n<p>print(formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__format__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9a\u4e49\u5982\u4f55\u5728\u4e0d\u540c\u683c\u5f0f\u4e0b\u8f93\u51fa\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u7684<code>format<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u3002\u901a\u8fc7\u638c\u63e1\u57fa\u672c\u7528\u6cd5\u3001\u6307\u5b9a\u4f4d\u7f6e\u3001\u547d\u540d\u5360\u4f4d\u7b26\u3001\u683c\u5f0f\u5316\u6570\u5b57\u3001\u5bf9\u9f50\u6587\u672c\u3001\u586b\u5145\u5b57\u7b26\u3001\u683c\u5f0f\u5316\u767e\u5206\u6bd4\u548c\u65e5\u671f\u65f6\u95f4\uff0c\u4ee5\u53ca\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u7c7b\u7b49\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u683c\u5f0f\u5316\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u529f\u80fd\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/><code>format<\/code>\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a<code>&quot;\u5b57\u7b26\u4e32\u5185\u5bb9{}&quot;.format(\u503c)<\/code>\u3002\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u82b1\u62ec\u53f7<code>{}<\/code>\u4ee3\u8868\u4e00\u4e2a\u5360\u4f4d\u7b26\uff0c<code>format<\/code>\u65b9\u6cd5\u4f1a\u5c06\u540e\u9762\u7684\u503c\u586b\u5165\u8fd9\u4e9b\u5360\u4f4d\u7b26\u3002\u4f8b\u5982\uff0c<code>&quot;Hello, {}&quot;.format(&quot;World&quot;)<\/code>\u5c06\u8f93\u51fa<code>&quot;Hello, World&quot;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728<code>format<\/code>\u65b9\u6cd5\u4e2d\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\uff1f<\/strong><br \/>\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u5728\u5360\u4f4d\u7b26\u4e2d\u6dfb\u52a0\u6570\u5b57\u6765\u6307\u5b9a\u53c2\u6570\u7684\u4f4d\u7f6e\uff0c\u4f8b\u5982<code>&quot;{0} is from {1}&quot;.format(&quot;Alice&quot;, &quot;Wonderland&quot;)<\/code>\u3002\u5173\u952e\u5b57\u53c2\u6570\u5219\u901a\u8fc7<code>{key}<\/code>\u7684\u5f62\u5f0f\u8fdb\u884c\u5f15\u7528\uff0c\u5982<code>&quot;{name} is from {place}&quot;.format(name=&quot;Alice&quot;, place=&quot;Wonderland&quot;)<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong><code>format<\/code>\u65b9\u6cd5\u4e0ef-string\u6709\u4f55\u4e0d\u540c\uff0c\u4ec0\u4e48\u65f6\u5019\u5e94\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\uff1f<\/strong><br \/><code>format<\/code>\u65b9\u6cd5\u9002\u7528\u4e8ePython 2.x\u548c3.x\u7248\u672c\uff0c\u800cf-string\u53ea\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u53ef\u7528\u3002f-string\u8bed\u6cd5\u66f4\u52a0\u7b80\u6d01\uff0c\u6027\u80fd\u66f4\u4f18\uff0c\u4f8b\u5982\uff0c<code>f&quot;{name} is from {place}&quot;<\/code>\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u901a\u5e38\u53d6\u51b3\u4e8e\u9879\u76ee\u7684Python\u7248\u672c\u548c\u4e2a\u4eba\u7f16\u7801\u98ce\u683c\u504f\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528format\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u63d2\u5165\u53d8\u91cf\u503c\u3001\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u662fforma [&hellip;]","protected":false},"author":3,"featured_media":1006889,"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\/1006880"}],"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=1006880"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006880\/revisions"}],"predecessor-version":[{"id":1006891,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006880\/revisions\/1006891"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1006889"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1006880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1006880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1006880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}