转载:https://mlln.cn/2018/05/19/python3 f-string格式化字符串的高级用法/
旧时代的格式化字符串
在Python 3.6之前,有两种将Python表达式嵌入到字符串文本中进行格式化的主要方法:%-formatting和str.format()。
1、 %-formatting
使用方法
字符串对象具有使用%运算符的内置操作,可以使用它来格式化字符串。以下是实践中的情况:
name = "Eric"
print("Hello, %s. " % name)
name = "Eric"
age = 74
print("Hello, %s. You are %s." % (name, age))
输出:
Hello, Eric.
Hello, Eric. You are 74.
缺点
上面刚刚看到的代码示例足够易读。但是,一旦开始使用几个参数和更长的字符串,代码将很快变得不太容易阅读。事情已经开始显得有点凌乱:
first_name = "Eric"
last_name = "Idle"
age = 74
profession = "comedian"
affiliation = "Monty Python"
print("Hello, %s %s. You are %s. You are a %s. You were a member of %s." %\
(first_name, last_name, age, profession, affiliation))
2、 str.format()
这种更新的工作方式是在Python 2.6中引入的。
使用方法
str.format()
是对%-formatting
的改进。它使用正常的函数调用语法,并且可以通过对要转换为字符串的对象的__format __()
方法进行扩展。
1)使用str.format()
,替换字段用大括号标记:
print("Hello, {}. You are {}.".format(name, age))
输出:
Hello, Eric. You are 74.
2)可以通过引用其索引来以任何顺序引用变量:
print("Hello, {1}. You are {0}-{0}.".format(age, name))
输出:
Hello, Eric. You are 74-74.
3)如果插入变量名称,则会获得额外的能够传递对象的权限,然后在大括号之间引用参数和方法:
person = {'name': 'Eric', 'age' : 74}
print ('Hello, {name}. You are {age}.' .format(name = person['name'], age = person [ 'age']))
输出:
Hello, Eric. You are 74.
4)也可以使用**来用字典来完成这个巧妙的技巧:
person = {'name': 'Eric', 'age' : 74}
print('Hello, {name}. You are {age}'.format(**person))
输出:
Hello, Eric. You are 74.
与f-string相比,str.format()
绝对是一个升级版本,但它并非总是好的。
缺点
使用str.format()
的代码比使用%-formatting
的代码更易读,但当处理多个参数和更长的字符串时,str.format()
仍然可能非常冗长。看看这个:
first_name = "Eric"
last_name = "Idle"
age = 74
profession = "comedian"
affiliation = "Monty Python"
print(("Hello, {first_name} {last_name}. You are {age}. " +
"You are a {profession}. You were a member of {affiliation}.") \
.format(first_name= first_name, last_name=last_name, age=age, \
profession=profession, affiliation=affiliation))
输出:
Hello, Eric Idle. You are 74. You are a comedian. You were a member of Monty Python.
3、f-Strings:一种改进Python格式字符串的新方法
F字符串可以节省很多的时间。他们确实使格式化更容易。他们自Python 3.6开始加入标准库。可以在PEP 498中阅读所有内容。
也称为“格式化字符串文字”,F字符串是开头有一个f的字符串文字,以及包含表达式的大括号将被其值替换。表达式在运行时进行渲染,然后使用__format__协议进行格式化。
使用方法
1)语法与str.format()使用的语法类似,但较少细节啰嗦。
name = "Eric"
age = 74
print(f"Hello, {name}. You are {age}.")
输出:
Hello, Eric. You are 74.
2) 使用大写字符串也有效
F"Hello, {name}. You are {age}."
任意表达式
1)由于f字符串是在运行时进行渲染的,因此可以将任何有效的Python表达式放入其中。
print(f'{2*23}')
输出:
46
2)可以调用函数
name = 'Eric'
f"{name.lower()} is funny."
输出:
eric is funny.
3)可以使用带有f字符串的类创建对象。想象一下你有以下类:
ass Comedian:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def __str__(self):
return f"{self.first_name} {self.last_name} is {self.age}."
def __repr__(self):
return f"{self.first_name} {self.last_name} is {self.age}. Surprise!"
__str __()和__repr __()方法处理对象如何呈现为字符串,因此您需要确保在类定义中包含至少一个这些方法。如果必须选择一个,请使用__repr __(),因为它可以代替__str __()。
__str __()返回的字符串是对象的非正式字符串表示,应该可读。__repr __()返回的字符串是官方表示,应该是明确的。调用str()和repr()比直接使用__str __()和__repr __()更好。
默认情况下,f字符串将使用__str __(),
new_comedian = Comedian("Eric", "Idle", "74")
f"{new_comedian}"
输出:
Eric Idle is 74.
但如果包含转换标志!r,则可以确保它们使用__repr __():
f"{new_comedian!r}"
输出:
Eric Idle is 74. Surprise!
4)多行f-string
message = (f"Hi {name}. "
"You are a {profession}. "
"You were in {affiliation}.")
message
输出:
Hi Eric. You are a comedian. You were in Monty Python.
但是如果你使用"""这将会发生什么:
message = f"""
Hi {name}.
You are a {profession}.
You were in {affiliation}.
"""
message
输出(plain):
'\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
name = "Eric"
age = 74
'%s is %s.' % (name, age)
输出(stream):
202 ns ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
(2)
%%timeit
name = "Eric"
age = 74
'{} is {}.'.format(name, age)
输出(stream):
244 ns ± 5.52 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
(3)
%%timeit
name = "Eric"
age = 74
'{name} is {age}.'
输出(stream):
14.4 ns ± 0.0121 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
你可以看到, 速度最快的就是f字符串.
Python f-Strings:Pesky细节
1、引号
您可以在表达式中使用各种类型的引号。只要确保在表达式中使用的f-字符串外部没有使用相同类型的引号即可。
以下写法都是正确的:
(1)
f"{'Eric Idle'}"
输出:
'Eric Idle'
(2)
f'{"Eric Idle"}'
输出:
'Eric Idle'
(3)
f"""Eric Idle"""
f'''Eric Idle'''
输出:
'Eric Idle'
'Eric Idle'
2、字典
说到引号,注意你在使用字典的时候。如果要为字典的键使用单引号,请记住确保对包含键的f字符串使用双引号。
以下代码是有效的:
comedian = {'name': 'Eric Idle', 'age': 74}
f"The comedian is {comedian['name']}, aged {comedian['age']}."
输出(plain):
'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字符串外部使用相同类型的引号,则第一个字典键开头的引号将被解释为字符串的结尾。
3、大括号
为了使字符串出现大括号,您必须使用双大括号:
f"{{74}}"
输出(plain):
'{74}'
但是,如果使用三个以上的大括号,则可以获得更多大括号:
f"{{{{74}}}}"
输出(plain):
'{{74}}'
4、反斜杠
正如您之前所看到的,您可以在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
5、lambda表达式
如果您需要使用lambda表达式,请记住,解析f-字符串的方式会稍微复杂一些。
如果!, :
或}
不在括号,大括号,括号或字符串中,则它将被解释为表达式的结尾。由于lambda使用:,这可能会导致一些问题:
f"{lambda x: x * 37 (2)}"
输出:
File "<fstring>", line 1
(lambda x)
^
SyntaxError: unexpected EOF while parsing
您可以通过将您的lambda嵌套在圆括号中来解决此问题:
f"{(lambda x: x * 37) (2)}"
输出(plain):
'74'