1 # 将字符串"{} {} {} {}"赋值给formatter 2 formatter = "{} {} {} {}" 3 4 # 将1,2,3,4分别传递到formatter的四个"{} {} {} {}"中并且打印 5 print(formatter.format(1,2,3,4)) 6 print(formatter.format("one","two","three","four")) 7 print(formatter.format(True,False,False,True)) 8 print(formatter.format(formatter,formatter,formatter,formatter)) 9 # 将"Try your", 10 # "Own text here", 11 # "Maybe a poem", 12 # "Or a song about fear" 13 # 分别传递到formatter的四个"{} {} {} {}"中,打印 14 print(formatter.format( 15 "Try your", 16 "Own text here", 17 "Maybe a poem", 18 "Or a song about fear" 19 ))
*本习题中,调用了format()函数,给这个函数传参数,
这时候所传递的参数会分别和formatter变量中的{}相匹配,
那么formatter变量中原本的{}就会被format()里所传的参数替换掉
print就把结果打印出来了
*而且一定数量要对应,N个{}对应N个参数
*只能是{}这个花括号,不能是别的符号,否则无法替换
*True和False不需要用“”是因为他们本身就是固定的布尔值,加了“”就变成了字符串了