python format用法详解

python format用法详解

https://www.cnblogs.com/mfrbuaa/p/4635450.html

复制代码

#常用方法:
print('{0},{1}'.format('zhangk', 32))
 
print('{},{},{}'.format('zhangk','boy',32))
 
print('{name},{sex},{age}'.format(age=32,sex='male',name='zhangk'))

print('{name},{gender},{age}'.format(age=3,gender='female',name='Alex'))

# 填充与对齐
# 填充常跟对齐一起使用
# ^、<、>分别是居中、左对齐、右对齐,后面带宽度
# :号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充
 
print('{:>8}'.format('zhang'))
print('{:0>8}'.format('zhang'))
print('{:a<8}'.format('zhang'))
print('{:p^10}'.format('zhang'))


# 精度与类型f
# 精度常跟类型f一起使用
print('{num:.3f}'.format(num=5))


# 其他类型
# 主要就是进制了,b、d、o、x分别是二进制、十进制、八进制、十六进制
print('{:b}'.format(15))
 
print('{:d}'.format(15))
 
print('{:o}'.format(15))
 
print('{:x}'.format(15))
 
# 用逗号还能用来做金额的千位分隔符
print('{:,}'.format(123456789))

print('{:,.2f}'.format(123456789))

##print_res:
##Alex,female,3
##5.000
##1111
##15
##17
##f
##123,456,789
##123,456,789.00

#我的案例:打印等腰三角形:

def print_trigle(n:int):
    format_str='{:^'+str(n)+'}'
    for i in range(1,n+1,2):
        print(format_str.format('*'*i))
print_trigle(21)

##         *          
##        ***         
##       *****        
##      *******       
##     *********      
##    ***********     
##   *************    
##  ***************   
## *****************  
##******************* 

复制代码

 

 

先看个实例

_formats = {
    'ymd': '{d.year}-{d.month}-{d.day}',
    'dmy': "{d.day}-{d.month}/{d.year}",
    'mdy': "{d.month}/{d.day}/{d.year}"
}


class Date(object):
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day

    def __format__(self, mode):
        if mode == '':
            mode = 'ymd'
        fmt = _formats[mode]
        return fmt.format(d=self)


d = Date(2018, 3, 30)
print format(d, 'ymd')
print format(d,'mdy')
print format(d,'dmy')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

—->>结果

2018-3-30
3/30/2018
30-3/2018
  • 1
  • 2
  • 3

小结一下:

1.format通过位置符号显示字符串

format后的括号内数字对应前边花括号的索引号
print "{0}:{1}:{1}".format(1,2)
--->>结果
1:2:2
  • 1
  • 2
  • 3
  • 4
print "{0}:{1}:{2}-{2}".format(1,2,3)
--->>结果
1:2:3-3
  • 1
  • 2
  • 3

2.使用名称显示字符串

print 'max:{max}\t\tmin:{min}'.format(min=2,max=4)
---->>结果
max:4       min:2
  • 1
  • 2
  • 3

3.通过属性显示字符串

class Book(object):
    def __init__(self,name,author,state,bookindex):
        self.name=name
        self.author=author
        self.state=state
        self.bookindex=bookindex

    def __str__(self):
        return 'book({d.name},{d.author},{d.state},{d.bookindex})'.format(d=self)
b=Book('python','laoli',1,'J90')
print b
----->>
book(python,laoli,1,J90)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

#4. 格式化元组的具体项

a=(1,2)
print 'x:{0[0]}\ty:{0[1]}'.format(a)
---->>>
x:1 y:2
  • 1
  • 2
  • 3
  • 4
goods = [
    ("001", 'computer', 100, 4000),
    ("002", 'apple', 1000, 4),
    ("003", 'xiaomi', 100, 3000),
]
fmt= '商品编号:{0[0]}\t商品名称:{0[1]}\t价格:{0[2]}\t数量:{0[3]}'.format
for i in map(fmt,goods):
    print i
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

—->>

商品编号:001    商品名称:computer   价格:100  数量:4000
商品编号:002    商品名称:apple  价格:1000 数量:4
商品编号:003    商品名称:xiaomi 价格:100  数量:3000
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值