'a1 = {} a2= {} a3= {}'.format('first','second','third') #{}不带参数
'a1 = first a2= second a3= third'
'a1 = {1} a2= {0} a3= {2}'.format('first','second','third') #{}带位置参数的
'a1 = second a2= first a3= third'
注意如果{}要带参数,可以将format看成一个函数输入的值是函数的参数,这些输入的值可以看成一个元组,{0} == tuple[0] 同样他们也不能越界
2.通过关键字参数
'your name is {name} , age is {age}'.format(name='jack',age=87)
'your name is jack , age is 87'
'your name is {name} , age is {age}'.format(age=87,name='jack') #同样和位置无关
'your name is jack , age is 87'
3.通过对象属性
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
p = Person('Tom',18)
'name = {p.name} age = {p.age}'.format(p=p)
'name = Tom age = 18'