来源:自学PHP网 时间:2019-08-07 16:33 作者:小飞侠 阅读:次
[导读] Python格式化字符串f-string概览(小结)...
|
简介 f-string,亦称为格式化字符串常量(formatted string literals),是Python3.6新引入的一种字符串格式化方法,该方法源于PEP 498 C Literal String Interpolation,主要目的是使格式化字符串的操作更加简便。f-string在形式上是以 While other string literals always have a constant value, formatted strings are really expressions evaluated at run time. f-string在功能方面不逊于传统的%-formatting语句和 用法 此部分内容主要参考以下资料: Python Documentation C Formatted String Literals Python Documentation C Format String Syntax PEP 498 C 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的大括号
>>> 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 "
若
>>> f"He said {"I'm Eric"}"
File "
大括号外的引号还可以使用
>>> f'''He\'ll say {"I'm Eric"}'''
"He'll say I'm Eric"
>>> f'''He'll say {"I\'m Eric"}'''
File "
f-string大括号外如果需要显示大括号,则应输入连续两个大括号
>>> f'5 {"{stars}"}'
'5 {stars}'
>>> f'{{5}} {"stars"}'
'{5} stars'
上面提到,f-string大括号内不能使用
>>> f"newline: {ord('\n')}"
File "
多行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采用 关于格式描述符的详细语法及含义可查阅Python官方文档,这里按使用时的先后顺序简要介绍常用格式描述符的含义与作用:
数字符号相关格式描述符
注:仅适用于数值类型。 数字显示方式相关格式描述符
|
注1:仅适用于数值类型。 注2:
宽度与精度相关格式描述符
注1: 示例:
>>> 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:若不指定 示例:
>>> 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'
格式类型相关格式描述符 基本格式类型
常用的特殊格式类型:标准库
综合示例
>>> 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'result is {lambda x: x ** 2 + 1 (2)}'
File "
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学php网。 |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com