python.format函数
str.format()
这是本人在学习过程中的笔记,详细参考请看链接
format函数可以接受无限个参数,位置可以不按顺序。
下面展示一些
>>>"{} {}".format("hello","world") #默认字符串位置
'hello world'
>>>"{1} {1} {0}".format("hello","world") #设置字符串位置
'world world hello'
也可以设置参数
print("网站名:{name},地址{url}".format(name = "MOOC",url ="www.icourse163.org" ))
#通过字典设置参数
site = {"name": "MOOC", "url": "www.icourse163.org" }
print("网站名:{name}, 地址:{url}".format(**site))
#通过列表索引输出参数
my_lsit = ['MOOC', 'www.icourse163.org']
print("网站名:{0[0]}, 地址:{0[1]}".format(my_list))
输出
网站名:MOOC,地址www.icourse163.org
网站名:MOOC, 地址:www.icourse163.org
网站名:MOOC, 地址:www.icourse163.org