标签:print 保留 字符 rgb int 字符串 default hit lin
%
%s字符串:%.4s,截取前4位字符
%d数字
%f浮点数:%.2f,表示保留2位小数
format
传递字符串:
" {0},{1},{2}" .format(str,str,str)
传递字符串、数字、浮点数负号:
"{:s} ,{:d}, {:f}".format(字符串, 数字, 浮点数)
传递字典:
"{a},{b}".format(**{"a":"1","b":"2"}) #传递字典,前面加**
"{a},{b}".format(a = "1",b = :"2")
传递元祖、列表:
{:s} , {:d}.format(*["a" ,2]) #传递列表或者元祖,前面加*
"{0[0]}, {0[1]}". fromat([1,2,3],[4,5,6])
1 a = "{0[0]}, {0[1]}". format([1, 2, 3], [4, 5, 6])
2 print(a)
3 #输出结果:
4 #1, 2
1 #传递列表:
2 aa = ["a", 1]
3 a = "{0}, {1}".format(*aa)
4 b = "{:s}, {:d}".format(*aa)
5 print(a)
6 print(b)
7 #传递字典:
8 aaa = {"e": "abd", "r": 2}
9 a1 = "{e}, {r}".format(**aaa)
10 b1 = "{e:s}, {r:d}".format(**aaa)
11 print(a1)
12 print(b1)
#输出结果:
#a, 1
#a, 1
#abc, 2
#abc, 2
#传递列表: aa = ["a", 1] a = "{0}, {1}".format(*aa) b = "{:s}, {:d}".format(*aa) print(a) print(b) #传递字典: aaa = {"e": "abd", "r": 2} a1 = "{e}, {r}".format(**aaa) b1 = "{e:s}, {r:d}".format(**aaa) print(a1) print(b1)
"{:d}, {:s}, {:b} ,{:o}, {:x}, {:X}, {:%}".format(15, "a", "15", "15", "15", "15", "15")
1 a = "{:d}, {:s}, {:b} ,{:o}, {:x}, {:X}, {:%}".format(15, "a", 15, 15, 15, 15, 15)
2 print(a)
3 #输出结果:
4 #15, a, 1111 ,17, f, F, 1500.000000%
:d:整形
:s:字符串
:b:10进制转换为2进制
:o:十进制转换为8进制
:x:十进制转换为16字母小写进制
:X:十进制转换为字母大写16进制
:%:转换百分比,保留6位,保留2位::.2%
python基础5:字符格式化
标签:print 保留 字符 rgb int 字符串 default hit lin
原文地址:https://www.cnblogs.com/ding-yuan/p/14878032.html