Python3学习3 格式化 % for

2023-01-31 01:01:00 学习 格式化 python3

一、%格式化

1.整数输出

%d  # 十进制
%o  # 八进制
%x  # 十六进制

print("%d"%23)  # 23
print("%o"%23)  # 27  
print("%x"%23)  # 17

2.浮点数输出

%f: 默认保留小数点后6位
%.2f 保留两位小数 四舍五入
%e: 科学计数法,默认保留小数点后6位
%g: 保证6位有效数字的前提下用小数表示,否则用科学计数法

print("%f"%2.3333)     #2.333300
print("%.2f"%2.8888)   #2.89 四舍五入
print("%e"%2.3333)     #2.333300e+00
print("%.3e"%2.3333)   #2.333e+00
print("%g"%2222.3333)  #2222.33
print("%g"%22888822.3333) #2.28888e+07
print("%.7g"%2222.8888) #2222.889 .7是有效数字的个数
print("%.3g"%2222.3333) #2.22e+03

3.字符串输出

print("%s" % "hello everyone")    # hello everyone
print("%65s" % "hello everyone")  # 右对齐,左侧空格补位
print("%-65s" % "hello everyone") #左对齐,右侧空格补位
print("%.5s" % "hello everyone")  #取前5个字符
print("%10.4s" % "hello everyone") #10位占位符,取4个字符右对齐      hell
print("%-10.4s" % "hello everyone") # hell

二、fORMat

1.位置匹配

1.1 不带编号

v1 = '{} {}'.format('hello','everyone')
print(v1)  # hello everyone

1.2 带数字编号(顺序可变)

v2 = '{0} {1}'.format('hello','everyone')
print(v2)  # hello everyone

v3 = '{1} {0} {0}'.format('hello','everyone')
print(v3)  # everyone hello hello

1.3 关键字

v4 = '{a} {b}'.format(a = 'hello',b = 'everyone')
print(v4)  # hello everyone

v5 = '{a} {b} {b}'.format(a = 'hello',b = 'everyone')
print(v5)  # hello everyone everyone

1.4 特殊

v6 = '{1} {0} {1}'.format(*'mnz')
print(v6)  # n m n

d1 = {'a1' : 21, 'b1' : 34}
v7 = 'test1:{a1}, test2:{b1}'.format(**d1)
print(v7)  # test1:21, test2:34

2.格式转换

# 将整数转换成对应的unicode字符
print('{:c}'.format(21016)) # 刘
# 十进制整数
print('{:d}'.format(20))  # 20
# 二进制整数
print('{:b}'.format(5))   # 101
# 八进制
print('{:o}'.format(23))  # 27
# 十六进制
print('{:x}'.format(23))  # 17
# 科学计数法
print('{:e}'.format(20))  # 2.000000e+01
print('{:g}'.format(20.34))  # 20.34
print('{:g}'.format(33320.34))  # 33320.3
print('{:.3g}'.format(33320.34))  # 3.33e+04

# n:d和g的合并
print('{:n}'.format(20))  # 20
print('{:n}'.format(20.23))  # 20.23
print('{:n}'.format(33320.23))  # 33320.2
print('{:3n}'.format(33320.23))  # 33320.2

print('{:f}'.format(20))  # 20.000000
print('{:.2f}'.format(23.8877))  #23.89

#数字乘100%,默认6位小数
print('{:%}'.format(20))  # 2000.000000%
print('{:.2%}'.format(20))  # 2000.00%

3. 正负号显示

print("{:f} and {:f}".format(2.345, -2.345))  # 2.345000 and -2.345000
print("{:+f} and {:f}".format(2.345, -2.345))  # +2.345000 and -2.345000
print("{:-f} and {:-f}".format(2.345, -2.345))  # 2.345000 and -2.345000

4. 对齐及位数不全

print("{} or {}".format('hello', 'everyone')) #左对齐 < 默认
print("{:20s} or {:>20s}".format('hello', 'everyone'))
print("{:20} or {:>20}".format('hello', 'everyone'))
print("{:^20} or {:^20}".format('hello', 'everyone'))
"""
输出:
hello or everyone
hello                or             everyone
hello                or             everyone
       hello         or       everyone      
"""

print("{:<20}".format('everyone'))  # <  左对齐
print("{:>20}".format('everyone'))  # >  右对齐
print("{:^20}".format('everyone'))  # ^  居中
print("{:*^21}".format('everyone')) # 填充
"""
输出:
everyone            
            everyone
      everyone      
******everyone*******
"""

print("{:0=20}".format(5123.12)) # = 只能用于数字,进行数字的补充
print("{:0<20}".format(5123.12))
print("{:0^20}".format(5123.12))
print("{0:>20.2f}".format(2.3456))
"""
输出:
00000000000005123.12
5123.120000000000000
0000005123.120000000
                2.35
"""

5. 通过下标或key值匹配参数

c1 = [2, 3, 4]
c2 = [5, 6, 7]
print('{} {} {}'.format(c1[0],c1[1],c1[2]))  # 2 3 4
print('{0[1]} {0[2]} {1[1]}'.format(c1, c2))  # 3 4 6

6. 逗号分隔

print('{:,}'.format(11556677842))  #11,556,677,842

相关文章