%格式符
%[(name)][flags][width].[precision]typecode
a = "%(name)s-----%(age)d "%{'name':'xx','age':20}print(a)
b = "%(name)+10s————————%(age)-10d————————"%{'name':'xx','age':20}print(b)
xx————————20 ————————
c = "------%(year) d******%(age)010d "%{'year':2016,'age':-20}print(c)
------ 2016******-000000020
.precision 可选,小数点后保留的位数
只保留两位小数
d = '--------%(p).2f'%{'p':1.23456}
d1 = '--------%(p)f'%{'p':1.23456}print(d)print(d1)
--------1.23
--------1.234560
typecode 必选
c,整数:将数字转换成其unicode对应的值,10进制范围为 0 <= i <= 1114111(py27则只支持0-255);字符:将字符添加到指定位置
o,将整数转换成 八 进制表示,并将其格式化到指定位置
x,将整数转换成十六进制表示,并将其格式化到指定位置
e ='***%c***%o***%x'%(65,15,15)print(e)
***A***17***f
e,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(小写e)
E,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(大写E)
f = '-----%(num)e------%(num)E'%{'num':1000000000}print(f)
-----1.000000e+09------1.000000E+09
g,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是e;)
G,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是E;)
g = '-----%(num)g------%(num1)G'%{'num':1000000000,'num1':100}print(g)
-----1e+09------100
%,当字符串中存在格式化标志时,需要用 %%表示一个百分号(类似于转意效果)
s = 'aaa %'print(s)
s1 = 'aaa %s %%'%('bbb')print(s1)
aaa %
aaa bbb %
format方式
[[fill]align][sign][#][0][width][,][.precision][type]
s1 ='---{:*^20s}----'.format('welcome')print(s1)
s2 ='---{:*>20s}----'.format('welcome')print(s2)
s3 ='---{:*<20s}----'.format('welcome')print(s3)
---******welcome*******----
---*************welcome----
---welcome*************----
a1 = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{:c}".format(15, 15, 15, 15, 15, 15.87623,65)
a2 = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%},{1:c}".format(15,65)
a3 = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%},{cc:c}".format(num=15,cc=65)print(a1)print(a2)print(a3)
numbers: 1111,17,15,f,F, 1587.623000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
, 【可选】为数字添加分隔符,如:1,000,000
.precision 【可选】小数位保留精度
n = '---{:,d}----'.format(10000000)
n1 = '---{:.2f}----'.format(1.2345)print(n)print(n1)
---10,000,000----
---1.23----
format常用格式化
tp1 = "i am {}, age {}, {}".format("seven", 18, 'alex')
tp2 = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
tp3 = "i am {0}, age {1}, really {0}".format("seven", 18)
tp4 = "i am {0}, age {1}, really {0}".format(*["seven", 18])
tp5 = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
tp6 = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
tp7 = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
tp8 = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
tp9 = "i am {:s}, age {:d}".format(*["seven", 18])
tp10 = "i am {name:s}, age {age:d}".format(name="seven", age=18)
tp11 = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})print(tp1)print(tp2)print(tp3)print(tp4)print(tp5)print(tp6)print(tp7)print(tp8)print(tp9)print(tp10)print(tp11)
i am seven, age 18, alex
i am seven, age 18, alex
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am 1, age 2, really 3
i am seven, age 18, money 88888.100000
i am seven, age 18
i am seven, age 18
i am seven, age 18
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |