好消息是,F字符串在这里可以节省很多的时间。他们确实使格式化更容易。他们自Python 3.6开始加入标准库。您可以在PEP 498中阅读所有内容。
也称为“格式化字符串文字”,F字符串是开头有一个f的字符串文字,以及包含表达式的大括号将被其值替换。表达式在运行时进行渲染,然后使用__format__
协议进行格式化。与往常一样,Python文档是您想要了解更多信息的最佳读物。
以下是f-strings可以让你的生活更轻松的一些方法。
简单例子
语法与str.format()
使用的语法类似,但较少细节啰嗦。看看这是多么容易可读:
1 |
name = "Eric" |
'Hello, Eric. You are 74.'
使用大写字母F也是有效的:
1 |
F"Hello, {name}. You are {age}." |
'Hello, Eric. You are 74.'
你喜欢F格式化字符串吗?我希望在本文的最后,你会回答>>> F"{Yes!}"
。
任意表达式
由于f字符串是在运行时进行渲染的,因此可以将任何有效的Python表达式放入其中。这可以让你做一些漂亮的事情。
你可以做一些非常简单的事情,就像这样:
1 |
f"{2 * 37}" |
'74'
你可以调用函数
1 |
f"{name.lower()} is funny." |
'eric is funny.'
你甚至可以使用带有f字符串的类创建对象。想象一下你有以下类:
1 |
class Comedian: |
1 |
new_comedian = Comedian("Eric", "Idle", "74") |
'Eric Idle is 74.'
__str __()
和__repr __()
方法处理对象如何呈现为字符串,因此您需要确保在类定义中包含至少一个这些方法。如果必须选择一个,请使用__repr __()
,因为它可以代替__str __()
。
__str __()
返回的字符串是对象的非正式字符串表示,应该可读。__repr __()
返回的字符串是官方表示,应该是明确的。调用str()
和repr()
比直接使用__str __()
和__repr __()
更好。
默认情况下,f字符串将使用__str __()
,但如果包含转换标志!r
,则可以确保它们使用__repr __()
:
1 |
f"{new_comedian}" |
'Eric Idle is 74.'
1 |
f"{new_comedian!r}" |
'Eric Idle is 74. Surprise!'
多行f-string
你可以有多行字符串:
1 |
message = (f"Hi {name}. " |
'Hi Eric. You are a comedian. You were in Monty Python.'
但请记住,您没必要将f放在多行字符串的每一行的前面。以下代码也能work:
1 |
message = (f"Hi {name}. " |
'Hi Eric. You are a {profession}. You were in {affiliation}.'
但是如果你使用"""
这将会发生什么:
1 |
message = f""" |
'\n Hi Eric. \n You are a comedian. \n You were in Monty Python.\n '
性能
f字符串中的f也可以代表“速度快”。
f-字符串比%-formatting
和str.format()
都快。正如你已经看到的,f-字符串是运行时渲染的表达式,而不是常量值。以下是文档摘录:
“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. In Python source code, an f-string is a literal string, prefixed with f, which contains expressions inside braces. The expressions are replaced with their values.” (Source)
在运行时,大括号内的表达式将在其自己的作用域中进行求值,然后将其与其余字符串组合在一起。
以下是速度比较:
1 |
%%timeit |
202 ns ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1 |
%%timeit |
244 ns ± 5.52 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1 |
%%timeit |
14.4 ns ± 0.0121 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
你可以看到, 速度最快的就是f字符串.
Python f-Strings:Pesky细节
现在你已经知道了为什么F字符串很好,我确定你想要出去并开始使用它们。当你冒险进入这个勇敢的新世界时,请记住一些细节。
引号
您可以在表达式中使用各种类型的引号。只要确保在表达式中使用的f-字符串外部没有使用相同类型的引号即可。
以下写法都是正确的:
1 |
f"{'Eric Idle'}" |
'Eric Idle'
1 |
f'{"Eric Idle"}' |
'Eric Idle'
1 |
f"""Eric Idle""" |
'Eric Idle'
1 |
f'''Eric Idle''' |
'Eric Idle'
1 |
f"The \"comedian\" is {name}, aged {age}." |
'The "comedian" is Eric, aged 74.'
字典
说到引号,注意你在使用字典的时候。如果要为字典的键使用单引号,请记住确保对包含键的f字符串使用双引号。
以下代码是有效的:
1 |
comedian = {'name': 'Eric Idle', 'age': 74} |
'The comedian is Eric Idle, aged 74.'
但是,以下代码就是一个语法错误:
1 |
f'The comedian is {comedian['name']}, aged {comedian['age']}.' |
File "<ipython-input-40-cd7d8a3db23b>", line 1
f'The comedian is {comedian['name']}, aged {comedian['age']}.'
^
SyntaxError: invalid syntax
如果您在字典键周围使用与在f字符串外部使用相同类型的引号,则第一个字典键开头的引号将被解释为字符串的结尾。
大括号
为了使字符串出现大括号,您必须使用双大括号:
1 |
f"{{74}}" |
'{74}'
但是,如果使用三个以上的大括号,则可以获得更多大括号:
1 |
f"{{{{74}}}}" |
'{{74}}'
反斜杠
正如您之前所看到的,您可以在f字符串的字符串部分使用反斜杠转义符。但是,您不能使用反斜杠在f字符串的表达式部分中进行转义:
1 |
f"{\"Eric Idle\"}" |
File "<ipython-input-43-35cb9fe0ccc1>", line 1
f"{\"Eric Idle\"}"
^
SyntaxError: f-string expression part cannot include a backslash
lambda表达式
如果您需要使用lambda表达式,请记住,解析f-字符串的方式会稍微复杂一些。
如果!, :
或}
不在括号,大括号,括号或字符串中,则它将被解释为表达式的结尾。由于lambda使用:
,这可能会导致一些问题:
1 |
f"{lambda x: x * 37 (2)}" |
File "<fstring>", line 1
(lambda x)
^
SyntaxError: unexpected EOF while parsing
您可以通过将您的lambda嵌套在圆括号中来解决此问题:
1 |
f"{(lambda x: x * 37) (2)}" |
'74'