[翻译]python3中新的字符串格式化方法-----f-string

2019/04/10 10:10
阅读数 13

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; padding: 10px; word-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; line-height: 1.75; color: #595959; font-family: Optima-Regular, Optima, PingFangTC-Light, PingFangSC-light, PingFangTC-light; letter-spacing: 2px; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center;"><p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">从python3.6开始,引入了新的字符串格式化方式,f-字符串. 这使得格式化字符串变得可读性更高,更简洁,更不容易出现错误而且速度也更快.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在本文后面,会详细介绍f-字符串的用法. 在此之前,让我们先来复习一下python中字符串格式化的方法.</p> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">python中传统的字符串格式化方法.</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在python3.6之前,我们有两种方式可以用来格式化字符串.</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: #595959; list-style-type: circle;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">占位符+%的方式</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">str.format()方法</section></li></ul> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">首先复习一下这两种方式的使用方法以及其短板.</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #DEC6FB; color: #595959;">占位符+%的方式</span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">这种方式算是第0代字符串格式化的方法,很多语言都支持类似的字符串格式化方法. 在python的文档中,我们也经常看到这种方式.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">但是!!! BUT!!!</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">占位符+%的方式并不是python推荐的方式.</p> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">Note&nbsp;The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer&nbsp;formatted string literals, the&nbsp;str.format()&nbsp;interface, or&nbsp;template strings&nbsp;may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.(<a href="https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">Python3 doc</a>)</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">文档中也说了,这种方式对于元组等的显示支持的不够好. 而且很容易产生错误.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">而且不符合python代码简洁优雅的人设...</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>如何使用占位符+%的方式<span>」</span></strong></p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">如果你接触过其他的语言,这种方式使用起来会有一种诡异的亲切感,这种亲切感会让你抓狂,内心会暗暗的骂上一句,艹,又是这德行...(这句不是翻译,是我的个人感觉,从来都记不住那么多数据类型的关键字...)</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span>]: name=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric'</span> <br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">2</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Hello,%s'</span>%name <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">2</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Hello,Eric'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">如果要插入多个变量的话,就必须使用元组.像这样</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">3</span>]: name=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric'</span> <br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">4</span>]: age=<span class="hljs-number" style="color: #d19a66; line-height: 26px;">18</span> <br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">5</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Hello %s,you are %d.'</span>%(name,age) <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">5</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Hello Eric,you are 18.'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>为什么说占位符+%的方式<s>不是最好的办法</s>(个人认为是这种方式是一种最操蛋的操作)<span>」</span></strong></p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">上面有少量的变量需要插入到字符串的时候,这种办法还行. 但是一旦有很多变量需要插入到一个长字符串中...比如...</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">6</span>]: first_name = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Eric"</span> ...: last_name = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Idle"</span> ...: age = <span class="hljs-number" style="color: #d19a66; line-height: 26px;">74</span> ...: profession = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"comedian"</span> ...: affiliation = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Monty Python"</span>

<br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">7</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Hello, %s %s. You are %s. You are a %s. You were a member of %s."</span> % (first_name, last_name, age, profession, affiliation) <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">7</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Hello, Eric Idle. You are 74. You are a comedian. You were a member of Monty Python.'</span> </code></pre>

<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">像上面这个例子,代码可读性就很差了.(对读和写的人都是一种折磨...)</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #DEC6FB; color: #595959;">使用str.format()的方式</span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在python2.6之后,引入了str.format()函数,可以用来进行字符串的格式化. 它通过调用对象的__format__()方法(<a href="https://www.python.org/dev/peps/pep-3101/#controlling-formatting-on-a-per-type-basis" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">PEP3101</a>中定义)来将对象转化成字符串.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在str.format()方法中,通过花括号占位的方式来实现变量插入.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">8</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello,{}. You are {}.'</span>.format(name,age) <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">8</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello,Eric. You are 74.'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">甚至可以给占位符加索引.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">9</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello,{1}. You are {0}.'</span>.format(age,name) <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">9</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello,Eric. You are 74.'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">如果要在占位符中使用变量名的话,可以像下面这样</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">10</span>]: person={<span class="hljs-string" style="color: #98c379; line-height: 26px;">'name'</span>:<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'age'</span>:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">74</span>} <br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">11</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello,{name}. you are {age}'</span>.format(name=person[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'name'</span>],age=person[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'age'</span>]) <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">11</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello,Eric. you are 74'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">当然对于字典来说的话,我们可以使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">**</code>的小技巧.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">15</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello,{name}. you are {age}'</span>.format(**person) <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">15</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello,Eric. you are 74'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">str.format()方法对于%的方式来说已经是一种很大的提升了. 但是这并不是最好的方式.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>为什么format()方法不是最好的方式<span>」</span></strong> 相比使用占位符+%的方式,format()方法的可读性已经很高了. 但是同样的,如果处理含有很多变量的字符串的时候,代码会变得很冗长.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>first_name = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Eric"</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>last_name = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Idle"</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>age = <span class="hljs-number" style="color: #d19a66; line-height: 26px;">74</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>profession = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"comedian"</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>affiliation = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Monty Python"</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>print((<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Hello, {first_name} {last_name}. You are {age}. "</span> + <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">"You are a {profession}. You were a member of {affiliation}."</span>) \ <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span> .format(first_name=first_name, last_name=last_name, age=age, \ <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span> profession=profession, affiliation=affiliation)) <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Hello, Eric Idle. You are 74. You are a comedian. You were a member of Monty Python.'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">当然,我们也可以通过字典的方式直接传入一个字典来解决代码过长的问题. 但是,python3.6给我们提供了更便利的方式.</p> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">f-字符串,一种新的增强型字符串格式化方式</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">这种新的方式在<a href="https://www.python.org/dev/peps/pep-0498/" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">PEP498</a>中定义.(原文写到这里的时候,作者可能疯了,balabla说了一长串,冷静的我并没有翻译这些废话...) 这种方式也被叫做<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">formatted string literals</code>.格式化的字符串常亮...ummm...应该是这么翻译吧...</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">这种方式在字符串开头的时候,以f标识,然后通过占位符{}+变量名的方式来自动解析对象的__format__方法. 如果想了解的更加详细,可以参考<a href="https://docs.python.org/3/reference/lexical_analysis.html#f-strings" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">python文档</a></p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #DEC6FB; color: #595959;">一些简单的例子</span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>使用变量名作为占位符<span>」</span></strong></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">16</span>]: name = <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric'</span> <br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">17</span>]: age=<span class="hljs-number" style="color: #d19a66; line-height: 26px;">74</span> <br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">18</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">f'hello <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{name}</span>, you are <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{age}</span>'</span> <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">18</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello Eric, you are 74'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>这里甚至可以使用大写的F<span>」</span></strong></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">19</span>]: F<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello {name}, you are {age}'</span> <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">19</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello Eric, you are 74'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">你以为这就完了吗?</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">不!</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">事情远不止想象的那么简单...</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在花括号里甚至可以执行算数表达式</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">20</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">f'<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{<span class="hljs-number" style="color: #d19a66; line-height: 26px;">2</span>*<span class="hljs-number" style="color: #d19a66; line-height: 26px;">37</span>}</span>'</span> <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">20</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'74'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">如果数学表达式都可以的话,那么在里面执行一个函数应该不算太过分吧...</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">22</span>]: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">def</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">to_lowercase</span><span class="hljs-params" style="line-height: 26px;">(input)</span>:</span> ...: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> input.lower() ...: <br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">23</span>]: name = <span class="hljs-string" style="color: #98c379; line-height: 26px;">'ERIC IDLE'</span> <br>In [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">24</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">f'<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{to_lowercase(name)}</span> is funny'</span> <br>Out[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">24</span>]: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'eric idle is funny'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">你以为这就完了吗?</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">不!</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">事情远不止想象的那么简单...</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">这玩意儿甚至可以用于重写__str__()和__repr__()方法.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"> <span class="hljs-class" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">class</span> <span class="hljs-title" style="color: #e6c07b; line-height: 26px;">Comedian</span>:</span> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">def</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">__init__</span><span class="hljs-params" style="line-height: 26px;">(self, first_name, last_name, age)</span>:</span> self.first_name = first_name self.last_name = last_name self.age = age

<span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">def</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">__str__</span><span class="hljs-params" style="line-height: 26px;">(self)</span>:</span>
    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{self.first_name}</span> <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{self.last_name}</span> is <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{self.age}</span>."</span>

<span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">def</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">__repr__</span><span class="hljs-params" style="line-height: 26px;">(self)</span>:</span>
    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{self.first_name}</span> <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{self.last_name}</span> is <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{self.age}</span>. Surprise!"</span>

<span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>>>> </span>new_comedian = Comedian(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Eric"</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Idle"</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">"74"</span>) <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>>>> </span><span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{new_comedian}</span>"</span><span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric Idle is 74.'</span> </code></pre>

<blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">关于__str__()方法和__repr__()方法. 这是对象的两个内置方法.<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">__str()__</code>方法用于返回一个便于人类阅读的字符串. 而<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">__repr__()</code>方法返回的是一个对象的准确释义. 这里暂时不做过多介绍. 如有必要,请关注公众号吾码2016(公众号:wmcoding)并发送<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">str_And_repr</code></p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">默认情况下,f-关键字会调用对象的__str__()方法. 如果我们想调用对象的__repr__()方法的话,可以使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">!r</code></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span><span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{new_comedian}</span>"</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric Idle is 74.'</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span><span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{new_comedian!r}</span>"</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric Idle is 74. Surprise!'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">更多详细内容可以参考<a href="https://mail.python.org/pipermail/python-ideas/2015-July/034726.html" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">这里</a></p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #DEC6FB; color: #595959;">多个f-字符串占位符</span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">同样的,我们可以使用多个f-字符串占位符.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>name = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Eric"</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>profession = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"comedian"</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>affiliation = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Monty Python"</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>message = ( <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Hi <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{name}</span>. "</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"You are a <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{profession}</span>. "</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"You were in <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{affiliation}</span>."</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span>) <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>message <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Hi Eric. You are a comedian. You were in Monty Python.'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">但是别忘了,在每一个字符串前面都要写上f</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">同样的,在字符串换行的时候,每一行也要写上f.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>message = <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Hi <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{name}</span>. "</span> \ <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"You are a <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{profession}</span>. "</span> \ <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"You were in <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{affiliation}</span>."</span>... <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>message <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Hi Eric. You are a comedian. You were in Monty Python.'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">但是如果我们使用"""的时候,不需要每一行都写.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>message = <span class="hljs-string" style="color: #98c379; line-height: 26px;">f""" <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span> Hi <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{name}</span>. <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span> You are a <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{profession}</span>. <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span> You were in <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{affiliation}</span>. <span class="hljs-meta" style="color: #61aeee; line-height: 26px;">... </span>"""</span> ... <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>message <span class="hljs-string" style="color: #98c379; line-height: 26px;">'\n Hi Eric.\n You are a comedian.\n You were in Monty Python.\n'</span> </code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">关于f-字符串的速度</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">f-字符串的f可能代表的含义是fast,因为f-字符串的速度比占位符+%的方式和format()函数的方式都要快.因为它是在运行时计算的表达式而不是常量值.(那为啥就快了呢...不太懂啊...)</p> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">“F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. <br>In Python source code, an f-string is a literal string, prefixed with&nbsp;f, which contains expressions inside braces. The expressions are replaced with their values.”(<a href="https://www.python.org/dev/peps/pep-0498/#abstract" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">PEP498</a>)</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">(官方文档,咱不敢翻,大意就是f-字符串是一个在运行时参与计算的表达式,而不是像常规字符串那样是一个常量值)</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在运行时,花括号内的表达式在其自己的作用域内求职,单号和字符串的部分拼接到一起,然后返回.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">下面我们来看一个速度的对比.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> timeit

time1 = timeit.timeit(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"""name = 'Eric'\nage =74\n'%s is %s'%(name,age)"""</span>,number=<span class="hljs-number" style="color: #d19a66; line-height: 26px;">100000</span>) time2 = timeit.timeit(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"""name = 'Eric'\nage =74\n'{} is {}'.format(name,age)"""</span>,number=<span class="hljs-number" style="color: #d19a66; line-height: 26px;">100000</span>) time3 = timeit.timeit(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"""name = 'Eric'\nage =74\nf'{name} is {age}'"""</span>,number=<span class="hljs-number" style="color: #d19a66; line-height: 26px;">100000</span>) </code></pre>

<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">从结果上看的话,f-字符串的方式速度要比其他两种快.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">0.030868000000000007 0.03721939999999996 0.0173276 </code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">f-字符串的一些细节问题</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>引号的问题<span>」</span></strong> 在f-字符串中,注意成对的引号使用.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric Idle'</span>}</span>"</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f'<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Eric Idle"</span>}</span>'</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"""Eric Idle"""</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f'''Eric Idle'''</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">以上这几种引号方式都是支持的. 如果说我们在双引号中需要再次使用双引号的时候,就需要进行转义了. <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">f"The \"comedian\" is {name}, aged {age}."</code></p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>字典的注意事项<span>」</span></strong></p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在字典使用的时候,还是要注意逗号的问题.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>comedian = {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'name'</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric Idle'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'age'</span>: <span class="hljs-number" style="color: #d19a66; line-height: 26px;">74</span>} <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span><span class="hljs-string" style="color: #98c379; line-height: 26px;">f"The comedian is <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{comedian[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'name'</span>]}</span>, aged <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{comedian[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'age'</span>]}</span>."</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span><span class="hljs-string" style="color: #98c379; line-height: 26px;">f'The comedian is <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{comedian[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'name'</span>]}</span>, aged <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{comedian[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'age'</span>]}</span>.'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">比如上面两条语句,第三句就是有问题的,主要还是引号引起的歧义.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>花括号<span>」</span></strong> 如果字符串中想使用花括号的话,就要写两个花括号来进行转义. 同理,如果想输出两个花括号的话,就要写四个...</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span><span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{{<span class="hljs-number">74</span>}</span>}"</span><span class="hljs-string">'{74}'</span> <span class="hljs-meta"><br>&gt;&gt;&gt; </span><span class="hljs-string">f"<span class="hljs-subst">{{{{<span class="hljs-number">74</span>}</span>}}}"</span> </span></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>反斜杠<span>」</span></strong> 反斜杠可以用于转义. 但是!!!BUT!!!在f-字符串中,不允许使用反斜杠.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span><span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{\<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Eric Idle\"}"</span> File <span class="hljs-string" style="color: #98c379; line-height: 26px;">"&lt;stdin&gt;"</span>, line <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{\<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Eric Idle\"}"</span> ^SyntaxError: f-string expression part cannot include a backslash </span></span></span></span></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">像上面这个的解决办法就是</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span>name = <span class="hljs-string" style="color: #98c379; line-height: 26px;">"Eric Idle"</span> <span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><br>&gt;&gt;&gt; </span><span class="hljs-string" style="color: #98c379; line-height: 26px;">f"<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{name}</span>"</span><span class="hljs-string" style="color: #98c379; line-height: 26px;">'Eric Idle'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>行内注释<span>」</span></strong> f-字符串表达式中不允许使用#符号.</p> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">总结和参考资料</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">我们依旧可以使用老的方式进行字符串格式化输出. 但是通过f-字符串,我们现在有了一种更便捷,更快,可读性更高的方式. 根据python教义,<a href="https://www.python.org/dev/peps/pep-0020/" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">Zen of Python</a>:</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>there should be one– and preferably only one –obvious way to do it.<span>」</span></strong> (编程还编出哲理来了...实在不会翻,有一种醍醐灌顶的感觉,内心浮现一个声音,卧槽!好有道理,仿佛自己升华了,但是仔细想想...这句话到底啥意思呢...)</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">更多的参考资料(我也只是写在这里,反正我是没有闲心看它的...):</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: #595959; list-style-type: circle;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><a href="https://www.python.org/dev/peps/pep-0502/" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">PEP502:String Interpolation - Extended Discussion</a></section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><a href="https://www.python.org/dev/peps/pep-0536/" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">PEP 536:Final Grammar for Literal String Interpolation</a></section></li></ul>

</section>

原文出处:https://www.cnblogs.com/thecatcher/p/12404444.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部