首页 > 其他 > 详细

format函数字符串格式化

时间:2020-08-31 22:53:18      阅读:77      评论:0      收藏:0      [点我收藏+]

1.描述

函数原型:format(value[, format_spec]) 参数意义: value: 需要被格式化的字符串 format_spec: 格式化的格式

2.实例

‘‘‘ 根据位置 ‘‘‘
>>>"{} {}".format("hello""Eleven")    # 不设置指定位置,按默认顺序
‘hello Eleven‘
 
>>> "{0} {1}".format("hello""Eleven")  # 设置指定位置
‘hello Eleven‘
 
>>> "{1} {0} {1}".format("hello""Eleven")  # 设置指定位置
‘Eleven hello Eleven‘
‘‘‘ 通过字典设置参数 ‘‘‘
site = {"name""Eleven-Blog""url""https://github.com/elevenwsy/eleven-blog"}
print("网站名:{name}, 地址 {url}".format(**site))    # 网站名:Eleven-Blog, 地址 https://github.com/elevenwsy/eleven-blog

‘‘‘ 通过列表索引设置参数 ‘‘‘
my_list = [‘Eleven-Blog‘‘https://github.com/elevenwsy/eleven-blog‘]
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list))  # 网站名:Eleven-Blog, 地址 https://github.com/elevenwsy/eleven-blog

format函数字符串格式化

原文:https://www.cnblogs.com/by-eleven/p/13592289.html

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