Python f-strings 3.6 版本新增加的字符串格式化功能
简介
Python 3.6 新版本增加的新功能 formatted string literals ,简称 f-strings,f-string,亦称为格式化字符串常量(formatted string literals),是Python3.6新引入的一种字符串格式化方法,该方法源于PEP 498 – Literal String Interpolation,主要目的是使格式化字符串的操作更加简便。f-string在形式上是以 f 或 F 修饰符引领的字符串(f’xxx’ 或 F’xxx’),以大括号 {} 标明被替换的字段;f-string在本质上并不是字符串常量,而是一个在运行时运算求值的表达式:
While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.
(与具有恒定值的其它字符串常量不同,格式化字符串实际上是运行时运算求值的表达式。)
—— Python Documentation
f-string在功能方面不逊于传统的%-formatting语句和str.format()函数,同时性能又优于二者,且使用起来也更加简洁明了,因此对于Python3.6及以后的版本,推荐使用f-string进行字符串格式化。
简单使用
这是一个让人兴奋的格式化字符串的方式,为什么这么说,先来看看效果
- f-string用大括号 {} 表示被替换字段,其中直接填入替换内容:
1
2
3
4
5
6
7>>> name = 'wxnacy'
>>> f'my name is {name}'
'my name is wxnacy'
>>> f'my name is {name} and length is {len(name)}'
'my name is wxnacy and length is 6'
>>> F'my name is {name} and length is {len(name)}'
'my name is wxnacy and length is 6' - 之所以叫f-strings ,就是要用 f 作为字符串的开头,就像 b’’, u’’ 那样,然后用 {} 中使用变量,甚至可以使用函数方法,就可以完成格式化,如果你也接触 Node 开发,就知道现在在 Javascript 中字符串的格式化通常是这样的
1
2
3
4> name = 'wxnacy'
'wxnacy'
> `my name is ${name} and length is ${name.length}`
'my name is wxnacy and length is 6表达式求值与函数调用
- f-string的大括号 {} 可以填入表达式或调用函数,Python会求出其结果并填入返回的字符串内:
1
2
3
4
5
6
7
8>>> f'A total number of {24 * 8 + 4}'
'A total number of 196'
>>> name = 'ERIC'
>>> f'My name is {name.lower()}'
'My name is eric'
>>> import math
>>> f'The answer is {math.log(math.pi)}'
'The answer is 1.1447298858494002'引号、大括号与反斜杠
f-string大括号内所用的引号不能和大括号外的引号定界符冲突,可根据情况灵活切换 ‘ 和 “ :若 ‘ 和 “ 不足以满足要求,还可以使用 ‘’’ 和 “””:1
2
3
4
5
6
7>>> f'I am {"Eric"}'
'I am Eric'
>>> f'I am {'Eric'}'
File "<input>", line 1
f'I am {'Eric'}'
^
SyntaxError: invalid syntax1
2
3
4
5
6
7
8
9
10
11
12
13
14>>> f"He said {"I'm Eric"}"
File "<input>", line 1
f"He said {"I'm Eric"}"
^
SyntaxError: invalid syntax
>>> f'He said {"I'm Eric"}'
File "<input>", line 1
f'He said {"I'm Eric"}'
^
SyntaxError: invalid syntax
>>> f"""He said {"I'm Eric"}"""
"He said I'm Eric"
>>> f'''He said {"I'm Eric"}'''
"He said I'm Eric"大括号外的引号还可以使用 \ 转义,但大括号内不能使用 \ 转义:
1
2
3
4
5>>> f'''He\'ll say {"I'm Eric"}'''
"He'll say I'm Eric"
>>> f'''He'll say {"I\'m Eric"}'''
File "<input>", line 1
SyntaxError: f-string expression part cannot include a backslashf-string大括号外如果需要显示大括号,则应输入连续两个大括号 :
1
2
3
4>>> f'5 {"{stars}"}'
'5 {stars}'
>>> f'{{5}} {"stars"}'
'{5} stars'上面提到,f-string大括号内不能使用 \ 转义,事实上不仅如此,f-string大括号内根本就不允许出现 \。如果确实需要 \,则应首先将包含 \ 的内容用一个变量表示,再在f-string大括号内填入变量名:
1
2
3
4
5
6>>> f"newline: {ord('\n')}"
File "<input>", line 1
SyntaxError: f-string expression part cannot include a backslash
>>> newline = ord('\n')
>>> f'newline: {newline}'
'newline: 10'多行f-string
f-string还可用于多行字符串:1
2
3
4
5
6>>> name = 'Eric'
>>> age = 27
>>> f"Hello!" \
... f"I'm {name}." \
... f"I'm {age}."
"Hello!I'm Eric.I'm 27."与原始字符串联合使用
1
2>>> fr'hello\nworld'
'hello\\nworld'支持表达式(对象)
1
2
3
4
5
6>>> spurs = {"Guard": "Parker", "Forward": "Duncan"}
>>> f"The {len(spurs)} players are: {spurs['Guard']} the guard, and {spurs['Forward']} the forward."
'The 2 players are: Parker the guard, and Duncan the forward.'
>>>
>>> f'Numbers from 1-10 are {[_ for _ in range(1, 11)]}'
'Numbers from 1-10 are [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]'
自定义格式:对齐、宽度、符号、补零、精度、进制等
f-string采用 {content:format} 设置字符串格式,其中 content 是替换并填入字符串的内容,可以是变量、表达式或函数等,format 是格式描述符。采用默认格式时不必指定 {:format},如上面例子所示只写 {content} 即可
- 排版格式
1
2
3
4
5
6
7
8
9
10
11
12>>> spurs = {"Guard": "Parker", "Forward": "Duncan"}
>>> def show_players():
... print(f"{'Position':^10}{'Name':^10}")
... for player in spurs:
... print(f"{player:^10}{spurs[player]:^10}")
...
...
...
>>> show_players()
Position Name
Guard Parker
Forward Duncan - 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17>>> a = 123.456
>>> f'a is {a:8.2f}'
'a is 123.46'
>>> f'a is {a:08.2f}'
'a is 00123.46'
>>> f'a is {a:8.2e}'
'a is 1.23e+02'
>>> f'a is {a:8.2%}'
'a is 12345.60%'
>>> f'a is {a:8.2g}'
'a is 1.2e+02'
>>> s = 'hello'
>>> f's is {s:8s}'
's is hello '
>>> f's is {s:8.3s}'
's is hel ' - 综合示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20>>> a = 1234
>>> f'a is {a:^#10X}' # 居中,宽度10位,十六进制整数(大写字母),显示0X前缀
'a is 0X4D2 '
>>> b = 1234.5678
>>> f'b is {b:<+10.2f}' # 左对齐,宽度10位,显示正号(+),定点数格式,2位小数
'b is +1234.57 '
>>> c = 12345678
>>> f'c is {c:015,d}' # 高位补零,宽度15位,十进制整数,使用,作为千分分割位
'c is 000,012,345,678'
>>> d = 0.5 + 2.5j
>>> f'd is {d:30.3e}' # 宽度30位,科学计数法,3位小数
'd is 5.000e-01+2.500e+00j'
>>> import datetime
>>> e = datetime.datetime.today()
>>> f'the time is {e:%Y-%m-%d (%a) %H:%M:%S}' # datetime时间格式
'the time is 2018-07-14 (Sat) 20:46:02'lambda表达式
f-string大括号内也可填入lambda表达式,但lambda表达式的 : 会被f-string误认为是表达式与格式描述符之间的分隔符,为避免歧义,需要将lambda表达式置于括号()内:1
2
3
4
5
6
7
8
9>>> f'result is {lambda x: x ** 2 + 1 (2)}'
File "<input>", line 1
(lambda x)
^
SyntaxError: unexpected EOF while parsing
>>> f'result is {(lambda x: x ** 2 + 1) (2)}'
'result is 5'
>>> f'result is {(lambda x: x ** 2 + 1) (2):<+7.2f}'
'result is +5.00 '
与 format() 对比
f-strings 与 format() 格式化的语法完全一致,只是更加简洁
1 | >>> import datetime |
结论
可以想象如果你的项目版本全面提到 3.6 以后,f-strings 格式化方法一定是首选,因为它不但简洁,并且快,它的速度比format()快了一倍。
参考资料
Python格式化字符串f-string概览 - sunxb10的博客 - CSDN博客
python3 f-string格式化字符串的高级用法 - DataScience
Python f-strings 3.6 版本新增加的字符串格式化功能