#python: 格式化字符串 f() 用法
原文地址 blog.csdn.net
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/sunxb10/article/details/81036693
#简介
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 进行字符串格式化。
#用法
此部分内容主要参考以下资料:
- Python Documentation – Formatted String Literals
- Python Documentation – Format String Syntax
- PEP 498 – Literal String Interpolation
- Python 3’s f-Strings: An Improved String Formatting Syntax (Guide)
- python3 f-string 格式化字符串的高级用法
- Python 3: An Intro to f-strings
#简单使用
f-string 用大括号 {} 表示被替换字段,其中直接填入替换内容:
>>> name = 'Eric'
>>> f'Hello, my name is {name}'
'Hello, my name is Eric'
>>> number = 7
>>> f'My lucky number is {number}'
'My lucky number is 7'
>>> price = 19.99
>>> f'The price of this book is {price}'
'The price of this book is 19.99'
#表达式求值与函数调用
f-string 的大括号 {} 可以填入表达式或调用函数,Python 会求出其结果并填入返回的字符串内:
>>> f'A total number of {24 * 8 + 4}'
'A total number of 196'
>>> f'Complex number {(2 + 2j) / (2 - 3j)}'
'Complex number (-0.15384615384615388+0.7692307692307692j)'
>>> 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 大括号内所用的引号不能和大括号外的引号定界符冲突,可根据情况灵活切换 ' 和 ":
>>> f'I am {"Eric"}'
'I am Eric'
>>> f'I am {'Eric'}'
File "<stdin>", line 1
f'I am {'Eric'}'
^
SyntaxError: invalid syntax
若 ' 和 " 不足以满足要求,还可以使用 ''' 和 """:
>>> f"He said {"I'm Eric"}"
File "<stdin>", line 1
f"He said {"I'm Eric"}"
^
SyntaxError: invalid syntax
>>> f'He said {"I'm Eric"}'
File "<stdin>", 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"
大括号外的引号还可以使用 \ 转义,但大括号内不能使用 \ 转义:
>>> f'''He\'ll say {"I'm Eric"}'''
"He'll say I'm Eric"
>>> f'''He'll say {"I\'m Eric"}'''
File "<stdin>", line 1
SyntaxError: f-string expression part cannot include a backslash
f-string 大括号外如果需要显示大括号,则应输入连续两个大括号 {{ 和 }}:
>>> f'5 {"{stars}"}'
'5 {stars}'
>>> f'{{5}} {"stars"}'
'{5} stars'
上面提到,f-string 大括号内不能使用 \ 转义,事实上不仅如此,f-string 大括号内根本就不允许出现 \。如果确实需要 \,则应首先将包含 \ 的内容用一个变量表示,再在 f-string 大括号内填入变量名:
>>> f"newline: {ord('\n')}"
File "<stdin>", line 1
SyntaxError: f-string expression part cannot include a backslash
>>> newline = ord('\n')
>>> f'newline: {newline}'
'newline: 10'
#多行 f-string
f-string 还可用于多行字符串:
>>> name = 'Eric'
>>> age = 27
>>> f"Hello!" \
... f"I'm {name}." \
... f"I'm {age}."
"Hello!I'm Eric.I'm 27."
>>> f"""Hello!
... I'm {name}.
... I'm {age}."""
"Hello!\n I'm Eric.\n I'm 27."
#自定义格式:对齐、宽度、符号、补零、精度、进制等
f-string 采用 {content:format} 设置字符串格式,其中 content 是替换并填入字符串的内容,可以是变量、表达式或函数等,format 是格式描述符。采用默认格式时不必指定 {:format},如上面例子所示只写 {content} 即可。
关于格式描述符的详细语法及含义可查阅 Python 官方文档,这里按使用时的先后顺序简要介绍常用格式描述符的含义与作用:
#对齐相关格式描述符
<table><thead><tr><th>格式描述符</th><th>含义与作用</th></tr></thead><tbody><tr><td><code><</code></td><td>左对齐(字符串默认对齐方式)</td></tr><tr><td><code>></code></td><td>右对齐(数值默认对齐方式)</td></tr><tr><td><code>^</code></td><td>居中</td></tr></tbody></table>#数字符号相关格式描述符
<table><thead><tr><th>格式描述符</th><th>含义与作用</th></tr></thead><tbody><tr><td><code>+</code></td><td>负数前加负号(<code>-</code>),正数前加正号(<code>+</code>)</td></tr><tr><td><code>-</code></td><td>负数前加负号(<code>-</code>),正数前不加任何符号(默认)</td></tr><tr><td><code></code>(空格)</td><td>负数前加负号(<code>-</code>),正数前加一个空格</td></tr></tbody></table>注:仅适用于数值类型。
#数字显示方式相关格式描述符
<table><thead><tr><th>格式描述符</th><th>含义与作用</th></tr></thead><tbody><tr><td><code>#</code></td><td>切换数字显示方式</td></tr></tbody></table>注 1:仅适用于数值类型。
注 2:# 对不同数值类型的作用效果不同,详见下表:
#宽度与精度相关格式描述符
<table><thead><tr><th>格式描述符</th><th>含义与作用</th></tr></thead><tbody><tr><td><code>width</code></td><td>整数 <code>width</code> 指定宽度</td></tr><tr><td><code>0width</code></td><td>整数 <code>width</code> 指定宽度,开头的 <code>0</code> 指定高位用 <code>0</code> 补足宽度</td></tr><tr><td><code>width.precision</code></td><td>整数 <code>width</code> 指定宽度,整数 <code>precision</code> 指定显示精度</td></tr></tbody></table>注 1:0width 不可用于复数类型和非数值类型,width.precision 不可用于整数类型。
注 2:width.precision 用于不同格式类型的浮点数、复数时的含义也不同:用于 f、F、e、E 和 % 时 precision 指定的是小数点后的位数,用于 g 和 G 时 precision 指定的是有效数字位数(小数点前位数 + 小数点后位数)。
注 3:width.precision 除浮点数、复数外还可用于字符串,此时 precision 含义是只使用字符串中前 precision 位字符。
示例:
>>> 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 '
#千位分隔符相关格式描述符
<table><thead><tr><th>格式描述符</th><th>含义与作用</th></tr></thead><tbody><tr><td><code>,</code></td><td>使用<code>,</code>作为千位分隔符</td></tr><tr><td><code>_</code></td><td>使用<code>_</code>作为千位分隔符</td></tr></tbody></table>注 1:若不指定 , 或 _,则 f-string 不使用任何千位分隔符,此为默认设置。
注 2:, 仅适用于浮点数、复数与十进制整数:对于浮点数和复数,, 只分隔小数点前的数位。
注 3:_ 适用于浮点数、复数与二、八、十、十六进制整数:对于浮点数和复数,_ 只分隔小数点前的数位;对于二、八、十六进制整数,固定从低位到高位每隔四位插入一个 _(十进制整数是每隔三位插入一个 _)。
示例:
>>> a = 1234567890.098765
>>> f'a is {a:f}'
'a is 1234567890.098765'
>>> f'a is {a:,f}'
'a is 1,234,567,890.098765'
>>> f'a is {a:_f}'
'a is 1_234_567_890.098765'
>>> b = 1234567890
>>> f'b is {b:_b}'
'b is 100_1001_1001_0110_0000_0010_1101_0010'
>>> f'b is {b:_o}'
'b is 111_4540_1322'
>>> f'b is {b:_d}'
'b is 1_234_567_890'
>>> f'b is {b:_x}'
'b is 4996_02d2'
#格式类型相关格式描述符
基本格式类型
<table><thead><tr><th>格式描述符</th><th>含义与作用</th><th>适用变量类型</th></tr></thead><tbody><tr><td><code>s</code></td><td>普通字符串格式</td><td>字符串</td></tr><tr><td><code>b</code></td><td>二进制整数格式</td><td>整数</td></tr><tr><td><code>c</code></td><td>字符格式,按 unicode 编码将整数转换为对应字符</td><td>整数</td></tr><tr><td><code>d</code></td><td>十进制整数格式</td><td>整数</td></tr><tr><td><code>o</code></td><td>八进制整数格式</td><td>整数</td></tr><tr><td><code>x</code></td><td>十六进制整数格式(小写字母)</td><td>整数</td></tr><tr><td><code>X</code></td><td>十六进制整数格式(大写字母)</td><td>整数</td></tr><tr><td><code>e</code></td><td>科学计数格式,以 <code>e</code> 表示 <code>×10^</code></td><td>浮点数、复数、整数(自动转换为浮点数)</td></tr><tr><td><code>E</code></td><td>与 <code>e</code> 等价,但以 <code>E</code> 表示 <code>×10^</code></td><td>浮点数、复数、整数(自动转换为浮点数)</td></tr><tr><td><code>f</code></td><td>定点数格式,默认精度(<code>precision</code>)是 6</td><td>浮点数、复数、整数(自动转换为浮点数)</td></tr><tr><td><code>F</code></td><td>与 <code>f</code> 等价,但将 <code>nan</code> 和 <code>inf</code> 换成 <code>NAN</code> 和 <code>INF</code></td><td>浮点数、复数、整数(自动转换为浮点数)</td></tr><tr><td><code>g</code></td><td>通用格式,小数用 <code>f</code>,大数用 <code>e</code></td><td>浮点数、复数、整数(自动转换为浮点数)</td></tr><tr><td><code>G</code></td><td>与 <code>G</code> 等价,但小数用 <code>F</code>,大数用 <code>E</code></td><td>浮点数、复数、整数(自动转换为浮点数)</td></tr><tr><td><code>%</code></td><td>百分比格式,数字自动乘上 100 后按 <code>f</code> 格式排版,并加 <code>%</code> 后缀</td><td>浮点数、整数(自动转换为浮点数)</td></tr></tbody></table>常用的特殊格式类型:标准库 datetime 给定的用于排版时间信息的格式类型,适用于 date、datetime 和 time 对象
#综合示例
>>> 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 表达式置于括号 () 内:
>>> f'result is {lambda x: x ** 2 + 1 (2)}'
File "<fstring>", 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 '