字符串类型的格式化
一. format()基本格式
//“ {<参数序号>:<格式控制>}{<参数序号>:<格式控制>...}”.format(<参数1,参数二>...)
举例:
// #e4.1TextProgress Bar.py
import time
scale = 20
print("------执行开始------")
for i in range(scale+1):
a, b = '**' * i,'..' * (scale - i)
c = (i/scale)*100
print("%{:^3.0f}[{}->{}]" .format (c, a, b))
time.sleep(0.1)
print("------执行结束------")
//运行结果如下:
% 0 [->........................................]
% 5 [**->......................................]
%10 [****->....................................]
%15 [******->..................................]
%20 [********->................................]
%25 [**********->..............................]
%30 [************->............................]
%35 [**************->..........................]
%40 [****************->........................]
%45 [******************->......................]
%50 [********************->....................]
%55 [**********************->..................]
%60 [************************->................]
%65 [**************************->..............]
%70 [****************************->............]
%75 [******************************->..........]
%80 [********************************->........]
%85 [**********************************->......]
%90 [************************************->....]
%95 [**************************************->..]
%100[****************************************->]
二. 控制格式类型
填充、对齐、宽度、千位分隔符、精度、类型
1. 填充
任意单个字符
2. 对齐
<左 >右 ^中
3. 宽度
"{0:30}".format("fun")
4. 千位分隔符
就是一个逗号
5. 精度
小数点加一个数字表示浮点数精度
6.类型
# 来自https://www.cnblogs.com/suguangti/p/10808168.html
# 字符串类型(默认类型,忽视)
print('{0:s}'.format('abc')) # abc
print('{0:}'.format('abc')) # abc
# 整数类型
# b 二进制|c Unicode字符|d 十进制整数|还有很多比如o八进制|x十六进制...等等
print('{0:b}'.format(101)) # 1100101
print('{0:c}'.format(101)) # e
print('{0:d}'.format(666)) # 666
# 浮点数字类型
# e 科学计数(默认精度6位)
print('{0:e}'.format(31415926)) # 3.141593e+07
# f 定点记法(默认精度6位)
print('{0:f}'.format(3.1415926)) # 3.141593