pythonformat函數(shù)用法詳解:
1、位置
print("hello{0},thisis{1}.".format("world","python"))#根據(jù)位置下標(biāo)進(jìn)行填充
print("hello{},thisis{}.".format("world","python"))#根據(jù)順序自動(dòng)填充
print("hello{0},thisis{1}.{1}isanewlanguage.".format("world","python"))#同一參數(shù)可以填充多次
輸出:
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函數(shù)是一種格式化輸出字符串的函數(shù)(str.format),基本語法是通過{}和:來代替以前的%