{"id":352107,"date":"2021-02-10T10:58:47","date_gmt":"2021-02-10T02:58:47","guid":{"rendered":"http:\/\/4563.org\/?p=352107"},"modified":"2021-02-24T16:37:55","modified_gmt":"2021-02-24T08:37:55","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-python-%e7%9a%84-f-string-%e5%8f%af%e4%bb%a5%e8%bf%9e%e6%8e%a5%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%8e%e6%95%b0%e5%ad%97%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=352107","title":{"rendered":"\u4e3a\u4ec0\u4e48 Python \u7684 f-string \u53ef\u4ee5\u8fde\u63a5\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e3a\u4ec0\u4e48 Python \u7684 f-string \u53ef\u4ee5\u8fde\u63a5\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chinesehuazhou <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<blockquote>\n<p>\u672c\u6587\u51fa\u81ea\u201cPython \u4e3a\u4ec0\u4e48\u201d\u7cfb\u5217\uff0c\u5f52\u6863\u5728 Github \u4e0a\uff1ahttps:\/\/github.com\/chinesehuazhou\/python-whydo<\/p>\n<\/blockquote>\n<p>\u6beb\u65e0\u7591\u95ee\uff0cPython \u662f\u4e00\u95e8<strong>\u5f3a\u7c7b\u578b<\/strong>\u8bed\u8a00\u3002<strong>\u5f3a\u7c7b\u578b<\/strong>\u8bed\u8a00\u3002<strong>\u5f3a\u7c7b\u578b<\/strong>\u8bed\u8a00\uff01\uff08\u5173\u4e8e\u5f3a\u5f31\u7c7b\u578b\u8bdd\u9898\uff0c\u63a8\u8350\u9605\u8bfb\u8fd9\u7bc7 \u6280\u672f\u79d1\u666e\u6587\uff09<\/p>\n<p>\u8fd9\u5c31\u610f\u5473\u7740\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u901a\u5e38\u9700\u8981\u5148\u505a<strong>\u663e\u5f0f\u5730\u7c7b\u578b\u8f6c\u5316\uff0c<\/strong> \u7136\u540e\u624d\u80fd\u8fdb\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002<\/p>\n<p>\u4e0b\u9762\u4ee5\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u4e3a\u4f8b\uff0c\u770b\u770b\u5f3a\u884c\u64cd\u4f5c\u4f1a\u4ea7\u751f\u4ec0\u4e48\u7ed3\u679c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; \"Python \u732b\" + 666 Traceback (most recent call last):   File \"&lt;stdin&gt;\", line 1, in &lt;module&gt; TypeError: can only concatenate str (not \"int\") to str <\/code><\/pre>\n<p>\u5b83\u62a5\u7c7b\u578b\u9519\u8bef\u4e86\uff08 TypeError \uff09\uff0c<strong>\u8bf4\u5b57\u7b26\u4e32\u53ea\u80fd\u8fde\u63a5\uff08 concatenate \uff09\u5b57\u7b26\u4e32\uff0c\u4e0d\u80fd\u8fde\u63a5 int \u7c7b\u578b\u3002<\/strong> \u8fd9\u6b63\u662f\u5f3a\u7c7b\u578b\u8bed\u8a00\u7684\u57fa\u672c\u7ea6\u675f\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u5148\u628a\u6570\u5b57\u201c\u8f6c\u5316\u201d\u6210\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u518d\u6267\u884c\u201c+\u201d\u64cd\u4f5c\uff0c\u5c31\u4e0d\u4f1a\u62a5\u9519\u4e86\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; \"Python \u732b\" + str(666) 'Python \u732b 666' <\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u5bf9\u8bfb\u8005\u4eec\u6765\u8bf4\uff0c\u5e94\u8be5\u5e76\u4e0d\u96be\u7406\u89e3\u3002<\/p>\n<p>\u7531\u6b64\uff0c\u6211\u4eec\u8981\u5f15\u51fa\u4e00\u4e2a\u95ee\u9898\uff1a<strong>\u5982\u4f55\u5728\u4e0d\u4f5c\u663e\u5f0f\u7c7b\u578b\u8f6c\u5316\u7684\u60c5\u51b5\u4e0b\uff0c\u8fdb\u884c\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\u7c7b\u578b\u7684\u62fc\u63a5\u5462\uff1f<\/strong> <\/p>\n<p>\u5728\u300a\u8be6\u89e3 Python \u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u4e03\u79cd\u65b9\u5f0f\u300b\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u5b83\u68b3\u7406\u4e86\u4e03\u79cd\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u5199\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9010\u4e2a\u6765\u8bd5\u9a8c\u4e00\u4e0b\u3002<\/p>\n<blockquote>\n<p>\u51e0\u79cd\u5b57\u7b26\u4e32\u62fc\u63a5\u65b9\u5f0f\uff1a<\/p>\n<p>1 \u3001\u683c\u5f0f\u5316\u7c7b\uff1a%\u3001format()\u3001template<\/p>\n<p>2 \u3001\u62fc\u63a5\u7c7b\uff1a+\u3001()\u3001join()<\/p>\n<p>3 \u3001\u63d2\u503c\u7c7b\uff1af-string<\/p>\n<\/blockquote>\n<p>\u4e3a\u4e86\u8282\u7701\u7bc7\u5e45\uff0c\u6b64\u5904\u76f4\u63a5\u628a\u53ef\u4ee5\u987a\u5229\u62fc\u63a5\u7684 4 \u79cd\u5199\u6cd5\u7f57\u5217\u5982\u4e0b\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; \"%s %d\" % (\"Python \u732b\", 666) 'Python \u732b 666'  &gt;&gt;&gt; from string import Template &gt;&gt;&gt; s = Template('${s1}${s2}') &gt;&gt;&gt; s.safe_substitute(s1='Python \u732b',s2=666) 'Python \u732b 666'  &gt;&gt;&gt; \"Python \u732b{}\".format(666) 'Python \u732b 666'  &gt;&gt;&gt; num = 666 &gt;&gt;&gt; f\"Python \u732b{num}\" 'Python \u732b 666' <\/code><\/pre>\n<p>\u7b2c\u4e00\u79cd\u5199\u6cd5\uff08\u5373 % \u683c\u5f0f\u5316\uff09\u6765\u81ea\u53e4\u8001\u7684 C \u8bed\u8a00\uff0c\u5176\u4e2d\u7684\u201c%d\u201d\u662f\u4e00\u4e2a\u5360\u4f4d\u7b26\uff0c\u8868\u793a\u5b83\u5c06\u8981\u63a5\u6536\u4e00\u4e2a\u6574\u6570\uff0c\u5e76\u683c\u5f0f\u5316\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u7b2c\u4e8c\u548c\u7b2c\u4e09\u79cd\u5199\u6cd5\uff0c\u5b83\u4eec\u662f\u7b2c\u4e00\u79cd\u5199\u6cd5\u7684\u5347\u7ea7\u7248\uff0c\u4e0d\u540c\u7684\u662f\uff0c\u5b83\u4eec\u7684\u5360\u4f4d\u7b26\u662f\u901a\u7528\u578b\u7684\uff0c\u4e0d\u5fc5\u6307\u5b9a\u201c%s\u201d\u3001\u201c%d\u201d\u7b49\u7b49\u660e\u786e\u7684\u7c7b\u578b\u3002\u8fd9\u4e24\u79cd\u5199\u6cd5\u4e2d\uff0c\u6570\u5b57\u7c7b\u578b\u7684\u53c2\u6570\u88ab\u4f20\u7ed9\u7279\u5b9a\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff08\u5373 safe_substitute \u4e0e format \uff09\uff0c\u5728\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5185\u90e8\uff0c\u5b83\u4eec\u4f1a\u4f5c\u7c7b\u578b\u8f6c\u5316\u5904\u7406\u3002<\/p>\n<p>\u53ef\u4ee5\u8bf4\uff0c\u4e0a\u8ff0\u4e09\u79cd\u5199\u6cd5\u90fd\u4e0d\u96be\u7406\u89e3\uff0c\u5b83\u4eec\u7684\u610f\u56fe\u90fd\u6709\u8ff9\u53ef\u5faa\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u73b0\u5728\u518d\u770b\u770b\u6700\u540e\u4e00\u79cd\u5199\u6cd5\uff0c\u4e5f\u5c31\u662f f-string \u5199\u6cd5\uff0c\u4f3c\u4e4e\u5c31\u4e0d\u662f\u90a3\u4e48\u660e\u663e\u4e86\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u5b57\u7b26\u4e32\u5185\u90e8\uff0c\u5b83\u5e76\u6ca1\u6709\u50cf\u201c%\u683c\u5f0f\u5316\u201d\u90a3\u6837\u6307\u5b9a\u5360\u4f4d\u7b26\u7684\u7c7b\u578b\uff1b\u5176\u6b21\uff0c\u6240\u8981\u62fc\u63a5\u7684\u6570\u5b57\u5e76\u6ca1\u6709\u4f5c\u4e3a\u4efb\u4f55\u51fd\u6570\u7684\u53c2\u6570\u6765\u4f20\u9012\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u660e\u9762\u4e0a\u6839\u672c\u770b\u4e0d\u51fa\u4efb\u4f55\u8981\u4f5c\u7c7b\u578b\u8f6c\u5316\u7684\u610f\u56fe\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u6211\u4eec\u5df2\u77e5 Python \u662f\u5f3a\u7c7b\u578b\u8bed\u8a00\uff0c\u5df2\u77e5\u6570\u5b57\u7c7b\u578b\u7edd\u5bf9\u4e0d\u53ef\u80fd\u76f4\u63a5\u62fc\u63a5\u5230\u5b57\u7b26\u4e32\u91cc\uff0c\u56e0\u6b64\uff0c<strong>\u53ea\u80fd\u8bf4\u660e f-string \u8bed\u6cd5\u5728\u5e95\u5c42\u4f5c\u4e86\u67d0\u79cd\u7c7b\u578b\u8f6c\u5316\u7684\u64cd\u4f5c\uff01<\/strong> <\/p>\n<p>\u90a3\u4e48\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u518d\u63d0\u51fa\u4e00\u4e2a\u65b0\u7684\u95ee\u9898\uff1a<strong>f-string \u8bed\u6cd5\u5728\u5904\u7406\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\u65f6\uff0c\u662f\u5982\u4f55\u5b9e\u73b0\u6570\u5b57\u7684\u7c7b\u578b\u8f6c\u5316\u7684\u5462\uff1f<\/strong> <\/p>\n<p>\u4e5f\u8bb8\u6709\u7684\u8bfb\u8005\u4f1a\u731c\u60f3\u5b83\u662f\u8c03\u7528\u4e86\u5185\u7f6e\u7684 str() \u6216 repr()\uff08\u6216\u5b83\u4eec\u5bf9\u5e94\u7684\u9b54\u672f\u65b9\u6cd5__str__() \u4e0e __repr__()\uff09\uff0c\u4ece\u800c\u5b9e\u73b0\u7c7b\u578b\u8f6c\u5316\uff0c\u4f46\u662f\uff0c\u7b54\u6848\u5e76\u6ca1\u6709\u5982\u6b64\u7b80\u5355\uff01<\/p>\n<p>f-string \u8bed\u6cd5\u662f\u5728 Python 3.6 \u7248\u672c\u5f15\u5165\u7684\u3002\u4e3a\u4e86\u7701\u4e8b\uff0c\u6211\u4eec\u76f4\u63a5\u627e\u5230 PEP-498 \u6587\u6863\uff0c\u5728\u91cc\u9762\u67e5\u9605\u770b\u662f\u5426\u6709\u5173\u4e8e\u5b9e\u73b0\u539f\u7406\u7684\u7ebf\u7d22\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/68b02e3bgy1gns2a4h8rgj20q80dl3zl.jpg\" alt=\"\u4e3a\u4ec0\u4e48 Python \u7684 f-string \u53ef\u4ee5\u8fde\u63a5\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\uff1f\" \/><\/p>\n<p>\u6587\u6863\u5730\u5740\uff1a https:\/\/www.python.org\/dev\/peps\/pep-0498<\/p>\n<p>PEP \u91cc\u63d0\u5230\uff0cf-string \u7684\u8bed\u6cd5\u683c\u5f0f\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code>f'&lt;text&gt; { &lt;expression&gt; &lt;optional !s, !r, or !a&gt; &lt;optional : format specifier&gt; } &lt;text&gt; ...' <\/code><\/pre>\n<p>\u5176\u4e2d\uff0c\u82b1\u62ec\u53f7\u91cc\u7684\u5185\u5bb9\u5c31\u662f\u8981\u4f5c\u683c\u5f0f\u5316\u7684\u5185\u5bb9\uff0c\u9664\u53bb\u53ef\u9009\u7684\u201coptional\u201d\u90e8\u5206\u540e\uff0c\u201cexpression\u201d\u90e8\u5206\u5c31\u662f\u771f\u6b63\u8981\u5904\u7406\u7684\u5185\u5bb9\u3002\u5bf9\u5e94\u524d\u6587\u7684\u4f8b\u5b50\uff0c\u6570\u5b57 666 \u5c31\u662f\u4e00\u4e2a expression \u3002<\/p>\n<p>expression \u4f1a\u6309 __format__ \u534f\u8bae\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u4f46\u662f\u5e76\u4e0d\u4f1a\u76f4\u63a5\u8c03\u7528 __format__() \u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<p>\u6587\u6863\u4e0a\u6307\u51fa\uff0c<strong>\u5b9e\u9645\u7684\u6267\u884c\u8fc7\u7a0b\u7b49\u6548\u4e8e<code>type(value).__format__(value, format_spec)<\/code> \u6216\u8005 <code>format(value, format_spec)<\/code> \u3002<\/strong> <\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684 foramt() \u65b9\u6cd5\u8ddf Python \u5185\u7f6e\u7684 foramt() \u51fd\u6570\uff0c\u5b83\u4eec\u90fd\u4f1a\u8c03\u7528__format__() \u9b54\u672f\u65b9\u6cd5\uff0c\u6240\u4ee5\uff0c<strong>f-string \u5176\u5b9e\u662f\u524d\u6587\u4e2d format() \u683c\u5f0f\u5316\u5199\u6cd5\u7684\u5347\u7ea7\u7248\u3002<\/strong> <\/p>\n<p>\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>format_spec<\/code> \u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff0c\u800c<code>format(value, \"\")<\/code> \u7684\u6548\u679c\u7b49\u540c\u4e8e<code>str(value)<\/code> \uff0c\u56e0\u6b64\uff0c\u5728\u4e0d\u6307\u5b9a\u5176\u5b83 format_spec \u7684\u60c5\u51b5\u4e0b\uff0c<strong>\u53ef\u4ee5\u7b80\u5355\u5730\u8ba4\u4e3a f-string \u5c31\u662f\u8c03\u7528\u4e86 str() \u6765\u4f5c\u7684\u7c7b\u578b\u8f6c\u5316\u2026\u2026<\/strong> <\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u770b\u5230\u4e86 f-string \u7684\u5b9e\u73b0\u539f\u7406\uff0c\u660e\u767d\u4e86\u5b83\u5728\u62fc\u63a5\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\u65f6\uff0c\u6548\u679c\u7b49\u6548\u4e8e\u524d\u6587\u7684 format() \u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4e5f\u7b49\u6548\u4e8e\u4f7f\u7528 str() \u8fdb\u884c\u7c7b\u578b\u8f6c\u5316\u3002<\/p>\n<p>\u5199\u5728\u6700\u540e\uff1a\u672c\u6587\u5c5e\u4e8e\u201cPython \u4e3a\u4ec0\u4e48\u201d\u7cfb\u5217\uff08 Python \u732b\u51fa\u54c1\uff09\uff0c\u8be5\u7cfb\u5217\u4e3b\u8981\u5173\u6ce8 Python \u7684\u8bed\u6cd5\u3001\u8bbe\u8ba1\u548c\u53d1\u5c55\u7b49\u8bdd\u9898\uff0c\u4ee5\u4e00\u4e2a\u4e2a\u201c\u4e3a\u4ec0\u4e48\u201d\u5f0f\u7684\u95ee\u9898\u4e3a\u5207\u5165\u70b9\uff0c\u8bd5\u7740\u5c55\u73b0 Python \u7684\u8ff7\u4eba\u9b45\u529b\u3002\u66f4\u591a\u7cbe\u5f69\u6587\u7ae0\uff0c\u8bf7\u79fb\u6b65 Github \u67e5\u770b\uff0c\u9879\u76ee\u5730\u5740\uff1ahttps:\/\/github.com\/chinesehuazhou\/python-whydo<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48 Python \u7684 f-st&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/352107"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=352107"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/352107\/revisions"}],"predecessor-version":[{"id":352109,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/352107\/revisions\/352109"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=352107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=352107"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=352107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}