format:
用字符串往format里传值,必须遵循一一对应的关系,不一一对应就报错(通过大括号里的数字可以指定对应值,类似索引)
text1 = "name {}, age {},{}".format("unbrella",20,"666")
print(text1)
text2 = "name {1}, age {0},{2}".format(20,"unbrella","666")
print(text2)
text = "my name is {name} and I‘m {age} years old.I like {hobby}" text.format(name="umbrella", age=20, hobby="studying") print(text)
字典形式用“**”,列表形式用“*”
text = "my name is {name} and I‘m {age} years old.I like {hobby}"
text.format(**{"name":"umbrella","age":20,"hobby":"studying"}) print(text)
:s 字符串, :d 整数 , :f 浮点数 , :x十六进制(小写) , :X十六进制(大写), :%显示成百分比,默认六位小数
text = "my name is {:s} and I‘m {:d} years old.I like {:f}".format("umbralla",20,99.99) print(text)