- str.format() :对应取值
name="chenshan"
age=30
address="宜山路926号新思大厦15楼" print "my name is {},age is {},and address {}".format(name,age,address) print "my name is {name},age is {age},and address {address}".format(name="chenshan",age=30,address="上海") --------------------输出-----------------------
my name is chenshan,age is 30,and address 宜山路926号新思大厦15楼
my name is chenshan,age is 30,and address 上海
- str.zfill() :向数值的字符串表达左侧填充 0
str="chenshan"
print str.zfill(10)
--------------------输出-----------------------
00chenshan
- str.rjust() 、str.ljust() 和 str.center() 分别设置向右、向左、居中对齐
str="hello"
print str.rjust(15)
print str.ljust(15)
print str.center(15)
--------------------输出-----------------------
hello
hello
hello
- :格式化显示格式 :会限定该字段的最小宽度,比较整齐
stock={"苏宁云商": 100,"招商银行": 101,"保利地产": 102} for name,number in stock.items():
print "{0:30} ====>{1:10d}".format(name,number)
--------------------输出-----------------------
苏宁云商 ====> 100
招商银行 ====> 101
保利地产 ====> 102