1、位置
print("hello{0},thisis{1}.".format("world","python"))#根据位置下标进行填充
print("hello{},thisis{}.".format("world","python"))#根据顺序自动填充
print("hello{0},thisis{1}.{1}isanewlanguage.".format("world","python"))#同一参数可以填充多次
输出:
helloworld,thisispython.
helloworld,thisispython.
helloworld,thisispython.pythonisanewlanguage.
2、key
obj="world"
name="python"
print("hello{obj},thisis{name}.".format(obj=obj,name=name))
输出:
helloworld,thisispython.
3、列表
list=["world","python"]
print("hello{names[0]},thisis{names[1]}.".format(names=list))
输出:
helloworld,thisispython.
format函数是一种格式化输出字符串的函数(str.format),基本语法是通过{}和:来代替以前的%