发布时间:2019-07-23 09:40:11编辑:auto阅读(1857)
%d # 十进制
%o # 八进制
%x # 十六进制
print("%d"%23) # 23
print("%o"%23) # 27
print("%x"%23) # 17
%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
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
v1 = '{} {}'.format('hello','everyone')
print(v1) # hello everyone
v2 = '{0} {1}'.format('hello','everyone')
print(v2) # hello everyone
v3 = '{1} {0} {0}'.format('hello','everyone')
print(v3) # everyone hello hello
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
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
# 将整数转换成对应的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%
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
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
"""
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
print('{:,}'.format(11556677842)) #11,556,677,842
上一篇: [Python]简单几行代码带你完成Py
下一篇: 【python3】任务分配问题
47485
45787
36785
34318
28956
25590
24437
19607
19103
17628
5459°
6042°
5565°
5633°
6567°
5371°
5371°
5879°
5852°
7164°