亲宝软件园·资讯

展开

python字符串格式化

穆梓先生 人气:0

Python的字符串格式化有两种方式:%格式符方式,format方式

%格式符

%[(name)][flags][width].[precision]typecode

注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式

(name)      可选,用于选择指定的key

a = "%(name)s-----%(age)d "%{'name':'xx','age':20}
print(a)

执行结果:

xx-----20

flags          可选,可供选择的值有:

width         可选,占有宽度

name占10位,+,右对齐,age占10位,-,左对齐

b = "%(name)+10s————————%(age)-10d————————"%{'name':'xx','age':20}
print(b)

执行结果:

xx————————20        ————————

 空格,右对齐

 0,用0填充空白处

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    必选

e ='***%c***%o***%x'%(65,15,15)
print(e)

执行结果:

***A***17***f

f = '-----%(num)e------%(num)E'%{'num':1000000000}
print(f)

执行结果:

-----1.000000e+09------1.000000E+09

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]

fill           【可选】空白处填充的字符

align        【可选】对齐方式(需配合width使用)

width       【可选】格式化位所占宽度

s1 ='---{:*^20s}----'.format('welcome')
print(s1)
s2 ='---{:*>20s}----'.format('welcome')
print(s2)
s3 ='---{:*<20s}----'.format('welcome')
print(s3)

执行结果:

---******welcome*******----
---*************welcome----
---welcome*************----

#            【可选】对于二进制、八进制、十六进制,如果加上#,会显示 0b/0o/0x,否则不显示

三种方法表示

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

总结

加载全部内容

相关教程
猜你喜欢
用户评论