{"id":85470,"date":"2021-08-17T12:20:43","date_gmt":"2021-08-17T04:20:43","guid":{"rendered":"http:\/\/www.zhushiyao.com\/?p=85470"},"modified":"2021-08-17T12:21:12","modified_gmt":"2021-08-17T04:21:12","slug":"f354165777","status":"publish","type":"post","link":"http:\/\/www.zhushiyao.com\/?p=85470","title":{"rendered":"\u6b63\u786e\u4f7f\u7528Python f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u76847\u4e2a\u5c42\u7ea7"},"content":{"rendered":"<div class=\"bpp-post-content\">\n<p><span style=\"font-size: 14px; color: #888888;\">f-string\u662fPython 3.6\u63a8\u51fa\u7684\u4e00\u79cd\u7b80\u5355\u800c\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u6280\u672f\uff0c\u53ef\u4ee5\u4f18\u96c5\u5730\u8868\u8fbePython\u5b57\u7b26\u4e32\u3002\u9664\u975e\u60a8\u4ecd\u5728\u4f7f\u7528\u65e7\u7684 Python \u7248\u672c\uff0c\u5426\u5219\u5728\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65f6\uff0cf \u5b57\u7b26\u4e32\u7edd\u5bf9\u5e94\u8be5\u662f\u60a8\u7684\u9996\u9009\u3002\u56e0\u4e3a\u5b83\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u8ff7\u4f60\u8bed\u6cd5\u6ee1\u8db3\u60a8\u7684\u6240\u6709\u8981\u6c42\uff0c\u751a\u81f3\u8fd0\u884c\u5b57\u7b26\u4e32\u7684\u8868\u8fbe\u5f0f\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u8fd9\u9879\u6280\u672f\u4ece\u521d\u7ea7\u5230\u6df1\u5ea6\u76847\u4e2a\u5c42\u6b21\u3002\u5728\u4e86\u89e3\u5b83\u4eec\u4e4b\u540e\uff0c\u60a8\u53ef\u80fd\u4f1a\u6210\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5927\u5e08\u3002<\/span><\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"rich_pages wxw-img\" title=\"\u6b63\u786e\u4f7f\u7528Python f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u76847\u4e2a\u5c42\u7ea7\" src=\"http:\/\/www.zhushiyao.com\/wp-content\/uploads\/2021\/08\/6-1629174045.png\" alt=\"\u6b63\u786e\u4f7f\u7528Python f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u76847\u4e2a\u5c42\u7ea7\" \/><\/p>\n<p><span style=\"font-size: 14px; color: #888888;\">\u00a0<\/span><\/p>\n<h1><span style=\"color: #ffa900;\"><strong><span style=\"font-size: 14px;\">1. \u8f7b\u677e\u4ece\u53d8\u91cf\u663e\u793a\u503c<\/span><\/strong><\/span><\/h1>\n<p><span style=\"color: #888888; font-size: 14px;\">\u4f7f\u7528 f \u5b57\u7b26\u4e32\u53ea\u9700\u8981\u505a\u4e24\u4ef6\u4e8b\uff1a<\/span><\/p>\n<ul class=\"list-paddingleft-2\" style=\"list-style-type: circle;\">\n<li><span style=\"color: #888888; font-size: 14px;\">\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0\u4e00\u4e2a\u5c0f\u5199\u7684<\/span><span style=\"font-size: 14px; color: #d92142;\">f<\/span><span style=\"color: #888888; font-size: 14px;\">;<\/span><\/li>\n<li><span style=\"color: #888888; font-size: 14px;\">\u4f7f\u7528f\u5b57\u7b26\u4e32\u4e2d\u4ee5<\/span><span style=\"font-size: 14px; color: #d92142;\">{variable_name}<\/span><span style=\"color: #888888; font-size: 14px;\">\u63d2\u503c\u53d8\u91cf.<\/span><\/li>\n<\/ul>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">name\u00a0=\u00a0'Yang'<\/span><\/code><code><span class=\"code-snippet_outer\">title = 'full stack hacker'<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{name} is a {title}.')<\/span><\/code><code><span class=\"code-snippet_outer\">#\u00a0Yang\u00a0is\u00a0a\u00a0full\u00a0stack\u00a0hacker.<\/span><\/code><\/pre>\n<\/section>\n<p><span style=\"font-size: 14px; color: #888888;\">\u5982\u4e0a\u6240\u793a\uff0c\u5728 f \u5b57\u7b26\u4e32\u673a\u5236\u7684\u5e2e\u52a9\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u7b80\u5355\u4e14\u66f4\u5c11\u7684\u4ee3\u7801\uff0c\u4ee5\u4fbf\u5728\u5b57\u7b26\u4e32\u4e2d\u663e\u793a\u66f4\u591a\u4ee3\u7801\u3002\u5b83\u5b8c\u7f8e\u5730\u547c\u5e94\u4e86 Python \u7684\u7985\u5b97\u3002<\/span><\/p>\n<blockquote><p><span style=\"font-size: 14px;\">&#8220;\u7b80\u5355\u603b\u6bd4\u590d\u6742\u597d\u3002<\/span><\/p><\/blockquote>\n<h1><strong><span style=\"font-size: 14px; color: #ffa900;\">2. \u6570\u5b57\u683c\u5f0f\u5316<\/span><\/strong><\/h1>\n<p><span style=\"font-size: 14px; color: #888888;\">\u6709\u65f6\u4ec5\u4ec5\u663e\u793a\u539f\u59cb\u503c\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\uff0c\u4f46\u662f\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u53d8\u91cf\u901a\u5e38\u4e0d\u662f\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u4e3a\u53d8\u91cf\u53ef\u80fd\u5728\u5176\u4ed6\u5730\u65b9\u4f7f\u7528\u3002<\/span><span style=\"color: #888888; font-size: 14px;\">\u4e0d\u7528\u62c5\u5fc3\uff0cPython f\u5b57\u7b26\u4e32\u8fd8\u652f\u6301&#8221;\u683c\u5f0f\u89c4\u8303\u8ff7\u4f60\u8bed\u8a00&#8221;\uff0c\u5b83\u4f7f\u6211\u4eec\u80fd\u591f\u6839\u636e\u81ea\u5df1\u559c\u6b22\u7684\u65b9\u5f0f\u5728 f \u5b57\u7b26\u4e32\u4e2d\u683c\u5f0f\u5316\u503c\uff0c\u5c24\u5176\u662f\u6570\u5b57\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">pi = 3.1415926<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'Pi is approximately equal to {pi:.2f}')<\/span><\/code><code><span class=\"code-snippet_outer\"># Pi is approximately equal to 3.14<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\">id = 1  # need to print a 3-digit number<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"The id is {id:03d}\")<\/span><\/code><code><span class=\"code-snippet_outer\"># The id is 001<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\">N = 1000000000  # need to add separator<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'His networth is ${N:,d}')<\/span><\/code><code><span class=\"code-snippet_outer\"># His networth is $1,000,000,000<\/span><\/code><\/pre>\n<\/section>\n<p><span style=\"font-size: 14px; color: #888888;\">\u4ee5\u4e0a\u793a\u4f8b\u4ec5\u663e\u793a\u4e86\u51b0\u5c71\u4e00\u89d2\u3002\u5bf9\u4e8e\u683c\u5f0f\u89c4\u683c\u8bed\u6cd5\u7684\u5b8c\u6574\u5217\u8868\uff0c\u76f8\u5e94\u7684\u5b98\u65b9\u6587\u6863\u662f\u60a8\u6700\u597d\u7684\u670b\u53cb\u3002<\/span><\/p>\n<h1><span style=\"color: #ffa900;\"><strong><span style=\"font-size: 14px;\">3. \u6b63\u786e\u6253\u5370\u7279\u6b8a\u5b57\u7b26<\/span><\/strong><\/span><\/h1>\n<p><span style=\"font-size: 14px; color: #888888;\">\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 f \u5b57\u7b26\u4e32\u6253\u5370\u8fd9\u4e9b\u5b57\u7b26\u6216\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u5417\uff1f\u6bd4\u5982<\/span><span style=\"font-size: 14px; color: #ffa900;\">&#8221;<\/span><span style=\"font-size: 14px; color: #888888;\">\u548c<\/span><span style=\"font-size: 14px; color: #ffa900;\">{}<\/span><span style=\"font-size: 14px; color: #888888;\">\u3002<\/span><span style=\"color: #888888; font-size: 14px;\">\u662f\u7684\uff0c\u5f53\u7136\u3002\u4f46\u8bed\u6cd5\u6709\u70b9\u68d8\u624b\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u3002<\/span><\/p>\n<p><span style=\"color: #888888; font-size: 14px;\">\u00a0<\/span><\/p>\n<h2><span style=\"font-size: 14px; color: #007aaa;\">3.1 \u6253\u5370\u5f15\u53f7<\/span><\/h2>\n<p><span style=\"font-size: 14px; color: #888888;\">\u6b63\u5982\u6211\u4eec\u6240\u77e5\uff0c\u53cd\u659c\u7ebf\u662f\u5e38\u7528\u7684\u8f6c\u4e49\u5b57\u7b26\uff0c\u7528\u4e8e\u8c03\u7528\u5bf9\u5176\u4ee5\u4e0b\u5b57\u7b26\u7684\u66ff\u4ee3\u89e3\u91ca\u3002\u5bf9\u4e8e f \u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u9700\u8981\u6ce8\u610f\u4e00\u6761\u89c4\u5219\uff1a<\/span><span style=\"font-size: 14px; color: #ffa900;\">\u5728 f \u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u7684\u62ec\u53f7{}\u4e2d\u4e0d\u8d77\u4f5c\u7528<\/span><span style=\"font-size: 14px; color: #888888;\">\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">name = 'Yang'<\/span><\/code><code><span class=\"code-snippet_outer\"># Correct Way:<\/span><\/code><code><span class=\"code-snippet_outer\">print(f''{name}' is a full stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># 'Yang' is a full stack hacker.<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\"># \u9519\u8bef\u65b9\u5f0f:<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{'name'} is a full stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># SyntaxError: f-string expression part cannot include a backslash<\/span><\/code><\/pre>\n<\/section>\n<h2><span style=\"font-size: 14px; color: #007aaa;\">3.2 \u6253\u5370\u53cc\u62ec\u53f7{}<\/span><\/h2>\n<p><span style=\"font-size: 14px; color: #888888;\">\u7528 f\u5b57\u7b26\u4e32\u6253\u5370{}\u7684\u65b9\u6cd5\u662f\u4e0d\u540c\u7684, \u975e\u5e38\u5bb9\u6613\u51fabug\u3002\u8fd9\u6b21\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528\u53cd\u659c\u7ebf\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">name = 'Yang'<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\"># 1<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{name} is a full stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># 'Yang' is a full stack hacker.<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\"># 2<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{{name}} is a full stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># {name} is a full stack hacker.<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\"># 3<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{{{name}}} is a full stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># {Yang} is a full stack hacker.<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\"># 4<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{{{{name}}}} is a full stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># {{name}} is a full stack hacker.<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\"># 5<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{{{{{name}}}}} is a full stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># {{Yang}} is a full stack hacker.<\/span><\/code><\/pre>\n<\/section>\n<p><span style=\"font-size: 14px; color: #888888;\">\u5982\u4e0a\u4f8b\u6240\u793a\uff0c\u8be5\u53d8\u91cf\u662f\u4f5c\u4e3af-\u5b57\u7b26\u4e32\u7684\u62ec\u53f7\u8fd8\u662f\u53d8\u91cf\u5904\u7406\u53d6\u51b3\u4e8e\u5176\u5468\u56f4\u7684\u62ec\u53f7\u6570\u3002\u5982\u679c\u60a8\u4e0d\u77e5\u9053\u8fd9\u79cd\u5947\u602a\u7684\u673a\u5236\uff0c\u5219\u5bb9\u6613\u51fa\u73b0\u9519\u8bef\u3002<\/span><\/p>\n<p><span style=\"font-size: 14px; color: #888888;\">\u00a0<\/span><\/p>\n<h2><span style=\"font-size: 14px; color: #007aaa;\">3.3 \u6253\u5370\u53cd\u659c\u7ebf<\/span><\/h2>\n<p><span style=\"font-size: 14px; color: #888888;\">\u6253\u5370\u53cd\u659c\u7ebf<\/span><span style=\"font-size: 14px; color: #888888;\">\u5f88\u7b80\u5355\uff1a<\/span><span style=\"font-size: 14px; color: #ffa900;\">\u53ea\u9700\u4f7f\u7528\u53cc\u53cd\u659c\u7ebf\u6253\u5370<\/span><span style=\"font-size: 14px; color: #888888;\">\u3002\u4f46\u662f\u4e0d\u8981\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 f \u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u62ec\u53f7\u5f53\u4e2d\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">name = 'Yang'<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'\\{name}\\ is a full \\stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># Yang is a full stack hacker.<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\">#\u9519\u8bef\u7684<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{\\name\\} is a full \\stack hacker.')<\/span><\/code><code><span class=\"code-snippet_outer\"># SyntaxError: f-string expression part cannot include a backslash<\/span><\/code><\/pre>\n<\/section>\n<h1><span style=\"color: #ffa900;\"><strong><span style=\"font-size: 14px;\">4. \u5c0f\u5fc3\u6253\u5370\u5b57\u5178\u503c<\/span><\/strong><\/span><\/h1>\n<p><span style=\"font-size: 14px; color: #888888;\">\u5c06\u5b57\u5178\u7684\u503c\u5e94\u7528\u5230 f \u5b57\u7b26\u4e32\u4e2d\u4e5f\u5bb9\u6613\u51fa\u73b0\u9519\u8bef\u3002\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u4e0d\u540c\u7684\u5f15\u53f7\u6765\u63cf\u8ff0\u5b57\u5178\u952e\u548c f \u5b57\u7b26\u4e32\uff0c\u5982\u4e0b\u6240\u793a\u3002\u5982\u679cf\u5b57\u7b26\u4e32\u7528\u53cc\u5f15\u53f7\u8868\u793a\uff0c\u90a3\u4e48\u53d8\u91cf\u91cc\u7684\u5b57\u5178\u952e\u5fc5\u987b\u7528\u5355\u5f15\u53f7\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">Hacker = {'name': 'Yang'}<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"{Hacker['name']} is a hacker\")<\/span><\/code><code><span class=\"code-snippet_outer\"># Yang is a hacker<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{Hacker[\"name\"]} is a hacker')<\/span><\/code><code><span class=\"code-snippet_outer\"># Yang is a hacker<\/span><\/code><code><span class=\"code-snippet_outer\">print(f'{Hacker['name']} is a hacker')<\/span><\/code><code><span class=\"code-snippet_outer\"># \u8bed\u6cd5\u9519\u8bef SyntaxError: invalid syntax<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"{Hacker[\"name\"]} is a hacker\")<\/span><\/code><code><span class=\"code-snippet_outer\"># \u8bed\u6cd5\u9519\u8bef SyntaxError: invalid syntax<\/span><\/code><\/pre>\n<\/section>\n<p><span style=\"font-size: 14px; color: #888888;\">\u5982\u4e0a\u6240\u793a\uff0c\u5982\u679c\u6211\u4eec\u5bf9\u952e\u540d\u548c f \u5b57\u7b26\u4e32\u90fd\u4f7f\u7528\u76f8\u540c\u7684\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7, Python \u4f1a\u5bf9\u6211\u4eec\u7684\u4ee3\u7801\u611f\u5230\u56f0\u60d1, \u4ece\u800c\u62a5\u9519\u3002<\/span><\/p>\n<p><span style=\"font-size: 14px; color: #888888;\">\u00a0<\/span><\/p>\n<h1><span style=\"color: #ffa900;\"><strong><span style=\"font-size: 14px;\">5. \u6b63\u786e\u5904\u7406\u591a\u884c F \u5b57\u7b26\u4e32<\/span><\/strong><\/span><\/h1>\n<p><span style=\"font-size: 14px; color: #888888;\">\u4e3a\u4e86\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u66f4\u6613\u8bfb\uff0c\u6709\u5fc5\u8981\u4f7f\u7528\u591a\u884c\u4e66\u5199\u4e00\u957f\u4e32\u5b57\u7b26\u3002\u4f46\u5982\u679c\u662f f \u5b57\u7b26\u4e32\uff0c\u4e0d\u8981\u5fd8\u8bb0\u5728\u6bcf\u884c\u4e4b\u524d\u6dfb\u52a0f\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">ame = 'Yang'<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\"># \u9519\u8bef\u65b9\u5f0f<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"{name} is a hacker.\"<\/span><\/code><code><span class=\"code-snippet_outer\">      \"{name} is also a top writer.\"<\/span><\/code><code><span class=\"code-snippet_outer\">      \"{name} writes on Medium.\"<\/span><\/code><code><span class=\"code-snippet_outer\">      )<\/span><\/code><code><span class=\"code-snippet_outer\"># Yang is a hacker.{name} is also a top writer.{name} writes on Medium.<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\"># \u6b63\u786e\u65b9\u5f0f<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"{name} is a hacker.\"<\/span><\/code><code><span class=\"code-snippet_outer\">      f\"{name} is also a top writer.\"<\/span><\/code><code><span class=\"code-snippet_outer\">      f\"{name} writes on Medium.\"<\/span><\/code><code><span class=\"code-snippet_outer\">      )<\/span><\/code><code><span class=\"code-snippet_outer\"># Yang is a hacker.Yang is also a top writer.Yang writes on Medium.<\/span><\/code><\/pre>\n<\/section>\n<p><span style=\"color: #ffa900;\"><strong><span style=\"font-size: 14px;\">6. \u50cf\u5927\u5e08\u4e00\u6837\u663e\u793a\u65e5\u671f\u548c\u65f6\u95f4<\/span><\/strong><\/span><\/p>\n<p><span style=\"font-size: 14px; color: #888888;\">\u5982\u679c\u6211\u4eec\u9700\u8981\u6253\u5370\u65e5\u671f\u6216\u65f6\u95f4\uff0cf \u5b57\u7b26\u4e32\u5c06\u518d\u6b21\u544a\u8bc9\u6211\u4eec\u5b83\u662f\u591a\u4e48\u65b9\u4fbf:<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">from datetime import datetime<\/span><\/code><code><span class=\"code-snippet_outer\">today = datetime.today()<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"Today is {today}\")<\/span><\/code><code><span class=\"code-snippet_outer\"># Today is 2021-07-31 18:20:48.956829<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"Today is {today:%B %d, %Y}\")<\/span><\/code><code><span class=\"code-snippet_outer\"># Today is July 31, 2021<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"Today is {today:%m-%d-%Y}\")<\/span><\/code><code><span class=\"code-snippet_outer\"># Today is 07-31-2021<\/span><\/code><\/pre>\n<\/section>\n<p><span style=\"font-size: 14px; color: #888888;\">\u5982\u4e0a\u4f8b\u6240\u793a\uff0c\u5728 f \u5b57\u7b26\u4e32\u7684\u5e2e\u52a9\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6211\u4eec\u4efb\u4f55\u559c\u6b22\u7684\u683c\u5f0f\u6253\u5370\u65e5\u671f\u6216\u65f6\u95f4\u3002<\/span><\/p>\n<p><span style=\"color: #ffa900;\"><strong><span style=\"font-size: 14px; background-color: #ffffff; font-family: sohne, 'Helvetica Neue', Helvetica, Arial, sans-serif; text-align: left;\">7. \u8bc4\u4f30 F \u5b57\u7b26\u4e32\u5185\u7684\u8868\u8fbe\u5f0f<\/span><\/strong><\/span><\/p>\n<p><span style=\"font-size: 14px; color: #888888;\">\u5f53\u6211\u7b2c\u4e00\u6b21\u77e5\u9053 f \u5b57\u7b26\u4e32\u65f6\uff0c \u6211\u7b80\u76f4\u4e0d\u6562\u76f8\u4fe1\uff1a\u6211\u4eec\u53ef\u4ee5\u5728 f \u5b57\u7b26\u4e32\u5185\u8fd0\u884c Python \u8868\u8fbe\u5f0f\u3002\u5982\u679c\u662f\u771f\u7684\uff0c\u8fd8\u7b97\u662f\u5b57\u7b26\u4e32\u5417\uff1f<\/span><\/p>\n<p><span style=\"font-size: 14px; color: #888888;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: 14px; color: #888888;\">\u6211\u4ed4\u7ec6\u9605\u8bfb\u4e86 PEP 498\uff0c\u7ec8\u4e8e\u660e\u767d\u4e86\uff1a<\/span><\/p>\n<blockquote><p><span style=\"font-size: 14px; color: #888888;\">F \u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u8868\u8fbe\u5f0f\u5d4c\u5165\u5b57\u7b26\u4e32\u5b57\u9762\u7684\u65b9\u6cd5\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cf \u5b57\u7b26\u4e32\u5b9e\u9645\u4e0a\u662f\u5728\u8fd0\u884c\u65f6\u95f4\u8bc4\u4f30\u7684\u8868\u8fbe\u65b9\u5f0f\uff0c\u800c\u4e0d\u662f\u6052\u5b9a\u7684\u503c\u3002<\/span><\/p><\/blockquote>\n<p><span style=\"font-size: 14px; color: #888888;\">\u56e0\u6b64\uff0cf \u5b57\u7b26\u4e32\u4e0e\u666e\u901a\u5b57\u7b26\u4e32\u4e0d\u540c\uff0c\u6b64\u529f\u80fd\u8d4b\u4e88\u5b83\u66f4\u5927\u7684\u80fd\u529b\u3002<\/span><span style=\"color: #888888; font-size: 14px;\">\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5b83\u91cc\u9762\u8fd0\u884c\u4e00\u4e2a\u663e\u793a\u65f6\u95f4\u7684\u529f\u80fd\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">from datetime import datetime<\/span><\/code><code><span class=\"code-snippet_outer\">\n<\/span><\/code><code><span class=\"code-snippet_outer\">print(f\"Today is {datetime.today()}\")<\/span><\/code><code><span class=\"code-snippet_outer\"># Today is 2021-07-31 18:20:48.956829<\/span><\/code><\/pre>\n<\/section>\n<h1><\/h1>\n<h1><span style=\"color: #ffa900;\"><strong><span style=\"font-size: 14px;\">\u7ed3\u8bba<\/span><\/strong><\/span><\/h1>\n<p><span style=\"font-size: 14px; color: #888888;\">Python \u4e2d\u7684 f-string\u662f\u4e00\u4e2a\u4f1f\u5927\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6280\u672f\uff0c \u663e\u793a\u4e86 Python \u662f\u591a\u4e48\u4f18\u96c5\u3002\u5b83\u975e\u5e38\u5f3a\u5927\uff0c\u56e0\u4e3a\u5b83\u4e0d\u662f\u4e00\u4e2a\u666e\u901a\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u8868\u8fbe\u7684\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u5b83\u6709\u7279\u6b8a\u7684\u89c4\u5219\uff0c\u6211\u4eec\u9700\u8981\u8c28\u614e\u4f7f\u7528\u5b83\u3002<\/span><\/p>\n<p><span style=\"font-size: 14px; color: #888888;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: 14px; color: #007aaa;\">\u539f\u6587\uff1a7 Levels of Using F-Strings in Python | by Yang Zhou<\/span><\/p>\n<p><span style=\"font-size: 14px; color: #007aaa;\">\u7ffb\u8bd1\uff1a\u5927\u6c5f\u72d7<\/span><\/p>\n<p>\u8f6c\u81ea\uff1ahttps:\/\/mp.weixin.qq.com\/s\/DMf-a2BwdXKSKAXBYEoDcw<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>f-string\u662fPython 3.6\u63a8\u51fa\u7684\u4e00\u79cd\u7b80\u5355\u800c\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u6280\u672f\uff0c\u53ef\u4ee5\u4f18\u96c5\u5730\u8868\u8fbePython\u5b57\u7b26\u4e32\u3002 &hellip; <a href=\"http:\/\/www.zhushiyao.com\/?p=85470\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u6b63\u786e\u4f7f\u7528Python f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u76847\u4e2a\u5c42\u7ea7\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[8],"_links":{"self":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/85470"}],"collection":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85470"}],"version-history":[{"count":2,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/85470\/revisions"}],"predecessor-version":[{"id":85474,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/85470\/revisions\/85474"}],"wp:attachment":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85470"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}