python中使用format函数格式化字符串,具体方法如下:
1.通过字典设置参数
site = {"name": "张三", "age": "18"}
print("姓名:{name}, 年龄 {age}".format(**site))
输出结果为:
姓名:张三,年龄 18
2.通过列表索引设置参数
my_list = ['李四', '21']
print("姓名:{0[0]}, 年龄 {0[1]}".format(my_list))
输出结果为:
姓名:李四,年龄 21
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python format()函数如何格式化字符串