{"id":3286,"date":"2025-05-30T16:19:01","date_gmt":"2025-06-04T08:22:00","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3286"},"modified":"2025-06-04T16:19:01","modified_gmt":"2025-06-04T08:22:00","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84format%e5%87%bd%e6%95%b8%e7%b0%a1%e5%96%ae%e5%bf%ab%e9%80%9f%e5%9c%b0%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%ad%97%e4%b8%b2-4","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e7%9a%84format%e5%87%bd%e6%95%b8%e7%b0%a1%e5%96%ae%e5%bf%ab%e9%80%9f%e5%9c%b0%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%ad%97%e4%b8%b2-4\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Python \u5b57\u4e32\u683c\u5f0f\u5316\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 format() \u51fd\u6578\u7684\u6240\u6709\u7528\u6cd5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, format(), \u5b57\u4e32\u683c\u5f0f\u5316, Python \u6559\u5b78\"><\/p>\n<h1>Python \u7684 format() \u51fd\u6578<\/h1>\n<p>Python \u7684 `format()` \u51fd\u6578\u662f\u4e00\u500b\u6975\u5176\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u8b93\u958b\u767c\u8005\u4ee5\u7c21\u55ae\u7684\u65b9\u5f0f\u683c\u5f0f\u5316\u5b57\u4e32\uff0c\u4e26\u5c07\u5b57\u4e32\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u66ff\u63db\u70ba\u6240\u9700\u5167\u5bb9\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e `format()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u53ca\u5176\u9032\u968e\u61c9\u7528\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u78ba\u4fdd\u60a8\u80fd\u5920\u5728\u5be6\u969b\u958b\u767c\u4e2d\u6709\u6548\u904b\u7528\u3002<\/p>\n<h2>format() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`format()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c07\u5b57\u4e32\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u66ff\u63db\u70ba\u6307\u5b9a\u7684\u5167\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\nname = \"John\"\nprint(\"Hello, {name}!\".format(name=name))\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u8f38\u51fa\uff1a<\/p>\n<pre class=\"brush: python\">\nHello, John!\n<\/pre>\n<p>\u9019\u88e1\uff0c\u6211\u5011\u4f7f\u7528\u4e86\u5927\u62ec\u865f `{}` \u4f86\u6307\u5b9a\u8981\u66ff\u63db\u7684\u90e8\u5206\uff0c\u4e26\u5728 `format()` \u51fd\u6578\u4e2d\u50b3\u5165\u5c0d\u61c9\u7684\u8b8a\u6578\u3002<\/p>\n<h2>\u683c\u5f0f\u5316\u6578\u5b57<\/h2>\n<p>`format()` \u51fd\u6578\u4e5f\u53ef\u4ee5\u7528\u4f86\u683c\u5f0f\u5316\u6578\u5b57\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u63a7\u5236\u6578\u5b57\u7684\u5c0f\u6578\u4f4d\u6578\uff1a<\/p>\n<pre class=\"brush: python\">\nnum = 123.456789\nprint(\"The number is {num:.2f}\".format(num=num))\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u8f38\u51fa\uff1a<\/p>\n<pre class=\"brush: python\">\nThe number is 123.46\n<\/pre>\n<p>\u9019\u88e1\uff0c`: .2f` \u8868\u793a\u5c07\u6578\u5b57\u683c\u5f0f\u5316\u70ba\u6d6e\u9ede\u6578\uff0c\u4e26\u4fdd\u7559\u5169\u4f4d\u5c0f\u6578\u3002<\/p>\n<h2>\u683c\u5f0f\u5316\u65e5\u671f<\/h2>\n<p>\u9664\u4e86\u5b57\u4e32\u548c\u6578\u5b57\uff0c`format()` \u51fd\u6578\u9084\u53ef\u4ee5\u7528\u4f86\u683c\u5f0f\u5316\u65e5\u671f\uff1a<\/p>\n<pre class=\"brush: python\">\nimport datetime\ndate = datetime.datetime.now()\nprint(\"Today is {date:%Y-%m-%d}\".format(date=date))\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u8f38\u51fa\u7576\u524d\u65e5\u671f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\nToday is 2025-06-01\n<\/pre>\n<p>\u9019\u88e1\uff0c`%Y-%m-%d` \u7528\u65bc\u6307\u5b9a\u65e5\u671f\u7684\u986f\u793a\u683c\u5f0f\u3002<\/p>\n<h2>\u683c\u5f0f\u5316\u5b57\u5178<\/h2>\n<p>\u60a8\u9084\u53ef\u4ee5\u4f7f\u7528 `format()` \u51fd\u6578\u683c\u5f0f\u5316\u5b57\u5178\u4e2d\u7684\u503c\uff1a<\/p>\n<pre class=\"brush: python\">\nperson = {'name': 'John', 'age': 20}\nprint(\"{person[name]} is {person[age]} years old\".format(person=person))\n<\/pre>\n<p>\u9019\u6a23\u6703\u8f38\u51fa\uff1a<\/p>\n<pre class=\"brush: python\">\nJohn is 20 years old\n<\/pre>\n<h2>\u683c\u5f0f\u5316\u5217\u8868<\/h2>\n<p>`format()` \u51fd\u6578\u540c\u6a23\u652f\u6301\u5217\u8868\u683c\u5f0f\u5316\uff1a<\/p>\n<pre class=\"brush: python\">\nnumbers = [1, 2, 3]\nprint(\"The numbers are {numbers[0]}, {numbers[1]}, and {numbers[2]}\".format(numbers=numbers))\n<\/pre>\n<p>\u8a72\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u70ba\uff1a<\/p>\n<pre class=\"brush: python\">\nThe numbers are 1, 2, and 3\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`format()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u9748\u6d3b\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u683c\u5f0f\u5316\u5404\u7a2e\u4e0d\u540c\u985e\u578b\u7684\u6578\u64da\u3002\u5f9e\u5b57\u4e32\u5230\u6578\u5b57\u3001\u65e5\u671f\u3001\u5b57\u5178\u548c\u5217\u8868\uff0c`format()` \u51fd\u6578\u5e6b\u52a9\u6211\u5011\u4fdd\u6301\u7a0b\u5f0f\u78bc\u7684\u7c21\u6f54\u8207\u53ef\u8b80\u6027\u3002<\/p>\n<p>\u82e5\u60f3\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u5176\u4ed6\u529f\u80fd\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78](https:\/\/vocus.cc) \u4ee5\u64f4\u5c55\u60a8\u7684\u77e5\u8b58\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. format() \u51fd\u6578\u548c f-string \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>`format()` \u51fd\u6578\u662f\u8f03\u65e9\u7684\u5b57\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u800c f-string\uff08\u683c\u5f0f\u5316\u5b57\u4e32\u5b57\u9762\u91cf\uff09\u662f Python 3.6 \u53ca\u4ee5\u5f8c\u7248\u672c\u5f15\u5165\u7684\u8a9e\u6cd5\uff0c\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c0\u7684\u65b9\u5f0f\u4f86\u683c\u5f0f\u5316\u5b57\u4e32\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728 format() \u4e2d\u4f7f\u7528\u591a\u500b\u8b8a\u6578\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728\u5b57\u4e32\u4e2d\u4f7f\u7528\u591a\u500b\u5927\u62ec\u865f\u4e26\u5728 `format()` \u51fd\u6578\u4e2d\u50b3\u5165\u591a\u500b\u8b8a\u6578\uff0c\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\nprint(\"Hello, {name}. You are {age} years old.\".format(name=\"John\", age=30))\n<\/pre>\n<h3>3. format() \u51fd\u6578\u7684\u6027\u80fd\u5982\u4f55\uff1f<\/h3>\n<p>\u5728\u9700\u8981\u683c\u5f0f\u5316\u5927\u91cf\u5b57\u4e32\u6642\uff0c`format()` \u51fd\u6578\u7684\u6027\u80fd\u53ef\u80fd\u4e0d\u5982 f-string\uff0c\u4f46\u5728\u4e00\u822c\u60c5\u6cc1\u4e0b\uff0c\u5b83\u7684\u6027\u80fd\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Python\u7684Format()\u51fd\u6578\u4f86\u683c\u5f0f\u5316\u5b57\u4e32\uff0c\u4ee5\u7372\u5f97\u66f4\u597d\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\uff0c\u4e26\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3286","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-R0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3286"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3286\/revisions"}],"predecessor-version":[{"id":12878,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3286\/revisions\/12878"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}