{"id":2593,"date":"2025-06-01T17:39:30","date_gmt":"2025-06-04T10:01:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2593"},"modified":"2025-06-04T17:39:30","modified_gmt":"2025-06-04T10:01:29","slug":"python-f-strings-tutorial","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python-f-strings-tutorial\/","title":{"rendered":"2025 \u6700\u65b0 Python f-strings \u8a9e\u6cd5\u6559\u5b78\uff1a\u8b93\u7a0b\u5f0f\u78bc\u7c21\u6f54\u6613\u8b80\uff01"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, f-strings, Python\u8a9e\u6cd5, Python\u7de8\u7a0b, f-strings\u6982\u5ff5, Python\u7de8\u7a0b\u6548\u7387, Python 2025, Python\u6559\u5b78\"><\/p>\n<p>\u5728 Python \u4e2d\uff0cf-strings\uff08\u683c\u5f0f\u5316\u5b57\u4e32\uff09\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u8a9e\u6cd5\uff0c\u81ea Python 3.6 \u7248\u672c\u4ee5\u5f8c\u6b63\u5f0f\u5f15\u5165\u3002\u9019\u7a2e\u8a9e\u6cd5\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u4e0d\u50c5\u66f4\u7c21\u6f54\uff0c\u4e5f\u66f4\u6613\u65bc\u95b1\u8b80\u548c\u7dad\u8b77\u3002\u96a8\u8457 Python \u5728 2025 \u5e74\u7684\u6301\u7e8c\u767c\u5c55\uff0cf-strings \u7684\u4f7f\u7528\u5df2\u7d93\u6210\u70ba\u6700\u4f73\u5be6\u8e10\u4e4b\u4e00\u3002<\/p>\n<p>### f-strings \u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>\u4f7f\u7528 f-strings \u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5728\u5b57\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd `f`\uff0c\u7136\u5f8c\u5728\u5927\u62ec\u865f `{}` \u4e2d\u5d4c\u5165\u8b8a\u6578\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nname = &#8216;John&#8217;<br \/>\nprint(f&#8217;Hello, {name}!&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\uff0c\u6703\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\nHello, John!<br \/>\n&#8220;`<\/p>\n<p>### \u5d4c\u5165\u8868\u9054\u5f0f<\/p>\n<p>f-strings \u4e0d\u50c5\u53ef\u4ee5\u5d4c\u5165\u8b8a\u6578\uff0c\u9084\u53ef\u4ee5\u5d4c\u5165\u8868\u9054\u5f0f\uff1a<\/p>\n<p>&#8220;`python<br \/>\na = 5<br \/>\nb = 10<br \/>\nprint(f'{a} + {b} = {a + b}&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\n5 + 10 = 15<br \/>\n&#8220;`<\/p>\n<p>### \u5d4c\u5165\u51fd\u6578<\/p>\n<p>\u4f60\u751a\u81f3\u53ef\u4ee5\u5728 f-strings \u4e2d\u5d4c\u5165\u51fd\u6578\u7684\u8fd4\u56de\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef to_uppercase(s):<br \/>\n    return s.upper()<\/p>\n<p>name = &#8216;John&#8217;<br \/>\nprint(f'{to_uppercase(name)}&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\nJOHN<br \/>\n&#8220;`<\/p>\n<p>### \u932f\u8aa4\u6392\u9664\u63d0\u793a<\/p>\n<p>\u5728\u4f7f\u7528 f-strings \u6642\uff0c\u82e5\u9047\u5230\u554f\u984c\uff0c\u8acb\u6aa2\u67e5\u8b8a\u6578\u540d\u7a31\u662f\u5426\u6b63\u78ba\uff0c\u78ba\u4fdd\u5927\u62ec\u865f `{}` \u4e2d\u7684\u8868\u9054\u5f0f\u80fd\u6b63\u78ba\u8a08\u7b97\u3002\u6b64\u8a9e\u6cd5\u652f\u6301\u591a\u7a2e\u6578\u64da\u985e\u578b\uff0c\u82e5\u67d0\u500b\u8b8a\u6578\u672a\u5b9a\u7fa9\uff0c\u5247\u6703\u5f15\u767c `NameError`\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>f-strings \u9084\u652f\u6301\u683c\u5f0f\u5316\u6578\u5b57\u3001\u65e5\u671f\u7b49\uff0c\u8b93\u4f60\u53ef\u4ee5\u5728\u5b57\u4e32\u4e2d\u8f15\u9b06\u5448\u73fe\u66f4\u8907\u96dc\u7684\u8cc7\u6599\u3002\u4f8b\u5982\uff0c\u82e5\u4f60\u8981\u683c\u5f0f\u5316\u6d6e\u9ede\u6578\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<p>&#8220;`python<br \/>\npi = 3.14159<br \/>\nprint(f&#8217;\u5713\u5468\u7387\u7d04\u70ba {pi:.2f}&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u5c07\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\n\u5713\u5468\u7387\u7d04\u70ba 3.14<br \/>\n&#8220;`<\/p>\n<p>### \u9032\u4e00\u6b65\u5b78\u7fd2<\/p>\n<p>\u82e5\u4f60\u60f3\u6df1\u5165\u4e86\u89e3 Python \u7684\u5176\u4ed6\u7279\u6027\uff0c\u4f8b\u5982\u88dd\u98fe\u5668\u3001\u751f\u6210\u5668\u7b49\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78](https:\/\/vocus.cc\/article\/5f4f0d12d8250a001e4b4db2) \u4f86\u64f4\u5c55\u4f60\u7684\u77e5\u8b58\u8207\u6280\u80fd\u3002<\/p>\n<p>### \u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/p>\n<p>**Q1: f-strings \u652f\u63f4\u54ea\u4e9b\u8cc7\u6599\u985e\u578b\uff1f**<br \/>\nA1: f-strings \u652f\u63f4\u6240\u6709 Python \u8cc7\u6599\u985e\u578b\uff0c\u5305\u62ec\u5b57\u4e32\u3001\u6578\u5b57\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<p>**Q2: \u53ef\u4ee5\u5d4c\u5165\u51fd\u6578\u547c\u53eb\u55ce\uff1f**<br \/>\nA2: \u662f\u7684\uff0cf-strings \u53ef\u4ee5\u5d4c\u5165\u4efb\u4f55\u53ef\u8a08\u7b97\u7684\u8868\u9054\u5f0f\uff0c\u5305\u62ec\u51fd\u6578\u547c\u53eb\u3002<\/p>\n<p>**Q3: f-strings \u8207\u5176\u4ed6\u683c\u5f0f\u5316\u5b57\u4e32\u65b9\u6cd5\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA3: \u76f8\u8f03\u65bc\u820a\u7684 `%` \u683c\u5f0f\u5316\u6216 `str.format()` \u65b9\u6cd5\uff0cf-strings \u63d0\u4f9b\u4e86\u66f4\u7c21\u6f54\u7684\u8a9e\u6cd5\u53ca\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u5167\u5bb9\u4e0d\u50c5\u5305\u542b\u4e86\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u5be6\u7528\u7bc4\u4f8b\uff0c\u9084\u5f37\u8abf\u4e86\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\uff0c\u4e26\u4e14\u7b26\u5408 SEO \u7684\u6700\u4f73\u5be6\u8e10\u3002\u5e0c\u671b\u9019\u80fd\u5e6b\u52a9\u5230\u8b80\u8005\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0cf-strings\uff08\u683c\u5f0f\u5316\u5b57&#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-2593","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-FP","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2593","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=2593"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2593\/revisions"}],"predecessor-version":[{"id":13053,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2593\/revisions\/13053"}],"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=2593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}