Python/f-strings

f-strings 是 Python 3.6 新增的功能. 这里简要摘录一下 f-strings 语法避免遗忘.它的标准语法非常简单, 理解为待格式化字符串的 {} 将会被当作 Python 代码求值即可.

name = 'mohanson'
pi = 3.14

# 语法: 执行 python 代码
print(f'name: {name}')             # name: mohanson
print(f'name: {name.upper()}')     # name: MOHANSON
print(f'2 * pi = {2 * pi}')        # 2 * pi = 6.28

# 语法: 浮点数左右对齐
print(f'pi: {pi:0<10}')            # pi: 3.14000000
print(f'pi: {pi:0>10}')            # pi: 0000003.14
print(f'pi: {pi:0^10}')            # pi: 0003.14000

# 语法: 固定浮点数位数
print(f'pi: {pi:.1f}')             # pi: 3.1
print(f'pi: {pi:.2f}')             # pi: 3.14

# 语法: !r, !s, !a 替代 repr(), str(), ascii()
print(f'name: {name!r}')           # name: 'mohanson'
print(f'name: {name!s}')           # name: mohanson
print(f'name: {name!a}')           # name: 'mohanson'

参考