文章目录
一. “%”用法
1.1 整数
- %o 八进制
- %d 十进制
- %x 十六进制
>>> "%o" % 29
'35'
>>> "%d" % 29 # 常用
'29'
>>> "%x" % 29
'1d'
1.2 浮点数
- %f 浮点数: 保留小数点后6位有效数字
- %.3f 保留小数点后3位有效数字
- %e 保留小数点后6位有效数字,以指数形式输出
- %.3e 保留小数后3位有效数字,使用科学计数法
- %g 在保证有6位有效数字的前提下, 使用小数方式,否则使用科学计数法
- %.3g 保留3位有效数字,使用小数或科学计数法
>>> "%f" % 1.1
'1.100000'
>>> "%.3f" % 1.11111 # 常用
'1.111'
>>> "%e" % 1.1
'1.100000e+00'
>>> "%.3e" % 1.1
'1.100e+00'
>>> "%g" % 1.1
'1.1'
>>> "%g" % 1111.1111
'1111.11'
>>> "%.3g" % 1111.1111
'1.11e+03'
>>> "%.8g" % 1111.1111
'1111.1111'
1.3 字符串
- %s
- %10s: 右对齐,占位符10位
- %-10s: 左对齐,占位符10位
- %.2s: 截取2位字符串
- %10.2s: 10位占位符,截取两位字符串
>>> "%s" % "hello world" # 常用
'hello world'
>>> "%20s" % "hello world"
' hello world'
>>> "%-20s" % "hello world"
'hello world '
>>> "%.3s" % "hello world"
'hel'
>>> "%20.3s" % "hello world"
' hel'
>>> "%-20.3s" % "hello world"
'hel '
二. format用法(推荐使用)
2.1 位置,数字,关键字匹配
>>> "{} {}".format("hello", "world")
'hello world'
>>> "{0} {1} {0}".format("hello", "world")
'hello world hello'
>>> "{a} {b} {c}".format(a="hello",b="world",c="test") # 常用
'hello world test'
2.2 对象
>>> tup = ("hello", "world")
>>> "{0[0]} {0[1]}".format(tup)
'hello world'
>>> d = {"a": "hello", "b": "world"}
>>> "{d[a]} {d[b]}".format(d) # 常见的错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'd'
>>> "{0[a]} {0[b]}".format(d) # 进阶用法
'hello world'
>>>
2.3 进阶用法
- ‘b’ - 二进制。将数字以2为基数进行输出。
- ‘c’ - 字符。在打印之前将整数转换成对应的Unicode字符串。
- ‘d’ - 十进制整数。将数字以10为基数进行输出。
- ‘o’ - 八进制。将数字以8为基数进行输出。
- ‘x’ - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母。
- ‘e’ - 幂符号。用科学计数法打印数字。用’e’表示幂。
- ‘g’ - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。
- ‘n’ - 数字。当值为整数时和’d’相同,值为浮点数时和’g’相同。不同的是它会根据区域设置插入数字分隔符。
- ‘%’ - 百分数。将数值乘以100然后以fixed-point(‘f’)格式打印,值后面会有一个百分号。
>>> "{:%}".format(10)
'1000.000000%'
>>> "{:.2%}".format(0.02) # 百分比计算 常用
'2.00%'
>>> "{:10s} and {:10s}".format("hello", "world")
'hello and world '
>>> "{:10s} and {:>10s}".format("hello", "world")
'hello and world'
>>> "{:^10s} and {:^10s}".format("hello", "world")
' hello and world '
>>> "{} is {:.2f}".format(1.123, 1.123) # 限定小数位
'1.123 is 1.12'
>>> "{} is {:>10.2f}".format(1.123, 1.123)
'1.123 is 1.12'
>>> "{:*^30s}".format("test") # 居中对齐
'*************test*************'
>>> "{:*>30s}".format("test") # 右对齐
'**************************test'
>>> "{:*<30s}".format("test") # 左对齐
'test**************************'
2.3.1 带符号输出(不常用)
>>> "{:+f}; {:+f}".format(1.23, -1.23)
'+1.230000; -1.230000'
>>> "{:+.2f}; {:+.2f}".format(1.23, -1.23)
'+1.23; -1.23'
>>> "{:+.2f}; {:-.2f}".format(1.23, -1.23)
'+1.23; -1.23'
>>> "{:-.2f}; {:-.2f}".format(1.23, -1.23)
'1.23; -1.23'
>>> "{: .2f}; {: .2f}".format(1.23, -1.23) # 正数有个空位
' 1.23; -1.23'
2.3.2 日期处理
>>> import datetime
>>> d = datetime.datetime(2021,6,23,18,30,30)
>>> "{:%Y-%m-%d %H:%M:%S}".format(d)
'2021-06-23 18:30:30'
2.3.3 金融业常用, 钱数用逗号分隔
>>> "{:,}".format(1234567890)
'1,234,567,890'
2.3.4 特殊占位符
>>> "{!r} {!s}".format("test1","test2")
"'test1' test2"
>>>
2.3.5 变形用法 (个人常用)
>>> a = "hello"
>>> b = "world"
>>> f"{a} {b}"
'hello world'
>>> f"word is {a.upper()}"
'word is HELLO'
版权声明:本文为weixin_39791387原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。