Python format 格式化函数

自python2.6开始,新增了一种格式化字符串的函数str.format()
1)通过位置

> '{} {}'.format('a','b')
'a b'
>>> '{1} {0}'.format('a','b')
'b a'
>>> '{1} {0} {1}'.format('a','b')

2)通过列表

>>> list1 = ['a','b']
>>> '{0[1]} {0[0]}'.format(list1)
'b a'

3)通过关键字

>>> "one:{first},two:{second}".format(second='b',first='a')
'one:a,two:b'

4)通过字典

>>> dic = {'a':1,'b':2}
>>> "one:{a},two:{b}".format(**dic)
'one:1,two:2'

 

posted @ 2019-03-31 14:09  西瓜草莓甘蔗  阅读(114)  评论(0编辑  收藏  举报