{"id":2589,"date":"2025-05-28T17:47:46","date_gmt":"2025-06-04T10:01:45","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2589"},"modified":"2025-06-04T17:47:46","modified_gmt":"2025-06-04T10:01:45","slug":"python-string-format-tutorial","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python-string-format-tutorial\/","title":{"rendered":"\u8f15\u9b06\u638c\u63e1 Python \u5b57\u4e32\u683c\u5f0f\u5316\uff1a\u5168\u9762\u89e3\u6790 format() \u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, String, \u5b57\u4e32, format(), \u5b57\u4e32\u683c\u5f0f\u5316, Python String\u5b57\u4e32 format(), \u5b57\u4e32\u683c\u5f0f\u5316\u6280\u5de7\"><\/p>\n<p>\u5728 Python \u4e2d\uff0c\u5b57\u4e32\u683c\u5f0f\u5316\u662f\u4e00\u9805\u975e\u5e38\u91cd\u8981\u7684\u6280\u80fd\uff0c\u7279\u5225\u662f\u7576\u4f60\u9700\u8981\u6e05\u695a\u4e14\u6709\u6548\u5730\u5448\u73fe\u8cc7\u6599\u6642\u30022025 \u5e74\u7684\u6700\u4f73\u5be6\u8e10\u662f\u4f7f\u7528 f-string \u8a9e\u6cd5\uff0c\u4f46\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u50b3\u7d71\u7684 `format()` \u65b9\u6cd5\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u5347\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u8207\u7dad\u8b77\u6027\u3002<\/p>\n<p>### Python \u5b57\u4e32 format() \u8a9e\u6cd5<\/p>\n<p>`format()` \u65b9\u6cd5\u5141\u8a31\u4f60\u5728\u5b57\u4e32\u4e2d\u63d2\u5165\u8b8a\u6578\uff0c\u800c\u4e0d\u9700\u8981\u76f4\u63a5\u4f7f\u7528\u5b57\u4e32\u9023\u63a5\uff0c\u9019\u6a23\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u78bc\u66f4\u6574\u6f54\u3002\u4ee5\u4e0b\u662f\u5e7e\u500b\u5e38\u898b\u7684\u7528\u6cd5\uff1a<\/p>\n<p>#### \u57fa\u672c\u4f7f\u7528<\/p>\n<p>\u4f60\u53ef\u4ee5\u900f\u904e `format()` \u65b9\u6cd5\u5c07\u8b8a\u6578\u63d2\u5165\u5b57\u4e32\u4e2d\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nname = &#8216;John&#8217;<br \/>\nprint(&#8216;Hello, {}&#8217;.format(name))<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\nHello, John<br \/>\n&#8220;`<\/p>\n<p>#### \u591a\u500b\u8b8a\u6578<\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u540c\u6642\u4f7f\u7528\u591a\u500b\u8b8a\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\nname = &#8216;John&#8217;<br \/>\nage = 20<br \/>\nprint(&#8216;{} is {} years old&#8217;.format(name, age))<br \/>\n&#8220;`<\/p>\n<p>\u9019\u5c07\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\nJohn is 20 years old<br \/>\n&#8220;`<\/p>\n<p>#### \u4f7f\u7528\u7d22\u5f15<\/p>\n<p>\u4f7f\u7528\u7d22\u5f15\u53ef\u4ee5\u8b93\u4f60\u63a7\u5236\u8b8a\u6578\u7684\u9806\u5e8f\uff1a<\/p>\n<p>&#8220;`python<br \/>\nname = &#8216;John&#8217;<br \/>\nage = 20<br \/>\nprint(&#8216;{1} is {0} years old&#8217;.format(age, name))<br \/>\n&#8220;`<\/p>\n<p>\u8f38\u51fa\u7d50\u679c\u4ecd\u7136\u662f\uff1a<\/p>\n<p>&#8220;`<br \/>\nJohn is 20 years old<br \/>\n&#8220;`<\/p>\n<p>#### \u4f7f\u7528\u5b57\u5178<\/p>\n<p>\u82e5\u6709\u4e00\u500b\u5b57\u5178\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 `**` \u64cd\u4f5c\u7b26\u81ea\u52d5\u89e3\u5305\u5b57\u5178\u4f86\u63d2\u5165\u8b8a\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\nperson = {&#8216;name&#8217;: &#8216;John&#8217;, &#8216;age&#8217;: 20}<br \/>\nprint(&#8216;{name} is {age} years old&#8217;.format(**person))<br \/>\n&#8220;`<\/p>\n<p>\u8f38\u51fa\u5c07\u662f\uff1a<\/p>\n<p>&#8220;`<br \/>\nJohn is 20 years old<br \/>\n&#8220;`<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `format()` \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>&#8211; **\u4f4d\u7f6e\u932f\u8aa4**\uff1a\u82e5\u5fd8\u8a18\u63d0\u4f9b\u5c0d\u61c9\u7684\u8b8a\u6578\uff0c\u6703\u5c0e\u81f4 `IndexError`\u3002<br \/>\n&#8211; **\u9375\u932f\u8aa4**\uff1a\u4f7f\u7528\u5b57\u5178\u6642\uff0c\u82e5\u9375\u540d\u62fc\u5beb\u932f\u8aa4\uff0c\u6703\u5c0e\u81f4 `KeyError`\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u96a8\u8457 Python \u7684\u767c\u5c55\uff0c\u5f9e Python 3.6 \u958b\u59cb\uff0c\u63a8\u85a6\u4f7f\u7528 **f-string** \u9032\u884c\u5b57\u4e32\u683c\u5f0f\u5316\uff0c\u9019\u7a2e\u65b9\u6cd5\u66f4\u52a0\u7c21\u6f54\u4e14\u6548\u7387\u66f4\u9ad8\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nname = &#8216;John&#8217;<br \/>\nage = 20<br \/>\nprint(f'{name} is {age} years old&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u5beb\u6cd5\u4e0d\u50c5\u53ef\u8b80\u6027\u5f37\uff0c\u6027\u80fd\u4e5f\u66f4\u4f73\u3002<\/p>\n<p>### \u7d50\u8ad6<\/p>\n<p>\u5b78\u6703\u4f7f\u7528 Python \u7684\u5b57\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u80fd\u5920\u5927\u5e45\u63d0\u5347\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u8207\u7dad\u8b77\u6027\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1 `format()` \u65b9\u6cd5\u53ca\u5176\u61c9\u7528\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u67e5\u770b\u66f4\u591a Python \u6559\u5b78\u6587\u7ae0\uff0c\u4f8b\u5982 [Python \u683c\u5f0f\u5316\u5b57\u4e32\u7684\u6700\u4f73\u5be6\u8e10](https:\/\/vocus.cc) \u4ee5\u6df1\u5165\u7406\u89e3\u5b57\u4e32\u8655\u7406\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: Python \u4e2d\u7684\u5b57\u4e32\u683c\u5f0f\u5316\u6709\u5e7e\u7a2e\u65b9\u6cd5\uff1f**<br \/>\nA1: \u4e3b\u8981\u6709\u4e09\u7a2e\uff1a`%` \u683c\u5f0f\u5316\u3001`str.format()` \u65b9\u6cd5\uff0c\u548c Python 3.6 \u5f15\u5165\u7684 f-string\u3002<\/p>\n<p>**Q2: \u70ba\u4ec0\u9ebc\u9078\u64c7\u4f7f\u7528 f-string\uff1f**<br \/>\nA2: f-string \u66f4\u52a0\u7c21\u6f54\uff0c\u4e14\u5728\u6027\u80fd\u4e0a\u512a\u65bc\u5176\u4ed6\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u591a\u6b21\u683c\u5f0f\u5316\u7684\u60c5\u6cc1\u4e0b\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u8655\u7406\u5b57\u5178\u4e2d\u7684\u8b8a\u6578\u683c\u5f0f\u5316\uff1f**<br \/>\nA3: \u4f7f\u7528 `**` \u64cd\u4f5c\u7b26\u53ef\u5c07\u5b57\u5178\u89e3\u5305\uff0c\u76f4\u63a5\u5728\u5b57\u4e32\u4e2d\u5f15\u7528\u9375\u540d\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u5b57\u4e32\u683c\u5f0f\u5316\u662f\u4e00\u9805\u975e\u5e38\u91cd\u8981\u7684\u6280&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"open","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":[],"class_list":["post-2589","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-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-FL","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2589","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=2589"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2589\/revisions"}],"predecessor-version":[{"id":13054,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2589\/revisions\/13054"}],"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=2589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}