python format函数用法详解:

1、位置

print("hello {0}, this is {1}.".format("world", "python")) # 根据位置下标进行填充

print("hello {}, this is {}.".format("world", "python")) # 根据顺序自动填充

print("hello {0}, this is {1}. {1} is a new language.".format("world", "python")) # 同一参数可以填充多次

输出:

hello world, this is python.

hello world, this is python.

hello world, this is python. python is a new language.

2、key

obj = "world"

name = "python"

print("hello {obj}, this is {name}.".format(obj = obj, name = name))

输出:

hello world, this is python.

3、列表

list = ["world", "python"]

print("hello {names[0]}, this is {names[1]}.".format(names = list))

输出:

hello world, this is python.

format函数公式 扩展资料

format函数是一种格式化输出字符串的函数(str.format),基本语法是通过{}和:来代替以前的%

format函数公式 format函数

猜你喜欢

联系我们

联系我们

888-888

邮件:admin@lcrz.cn

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部