格式化字符串字面值 (简称为 f-字符串)在字符串前加前缀 f 或 F,通过 {expression} 表达式,把 Python 表达式的值添加到字符串内。
格式说明符是可选的,写在表达式后面,可以更好地控制格式化值的方式。下例将 pi 舍入到小数点后三位:
import math
print(f'The value of pi is approximately {math.pi:.3f}.')
'''
输出
The value of pi is approximately 3.142.
'''
在 : 后传递整数,为该字段设置最小字符宽度,常用于列对齐:
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
print(f'{name:10} ==> {phone:10d}')
'''
输出
Sjoerd ==> 4127
Jack ==> 4098
Dcab ==> 7678
'''
还有一些修饰符可以在格式化前转换值。 !a 应用 ascii() ,!s 应用 str(),!r 应用 repr():
animals = 'eels'
print(f'My hovercraft is full of {animals}.')
print(f'My hovercraft is full of {animals!r}.')
= 说明符可被用于将一个表达式扩展为表达式文本、等号再加表达式求值结果的形式。
bugs = 'roaches'
count = 13
area = 'living room'
print(f'Debugging {bugs=} {count=} {area=}')
'''
输出
Debugging bugs='roaches' count=13 area='living room'
'''