首页 > 编程语言 > 详细

Python format 格式化函数

时间:2019-03-31 15:07:52      阅读:127      评论:0      收藏:0      [点我收藏+]

自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

 

Python format 格式化函数

原文:https://www.cnblogs.com/tongtong123/p/10631216.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!