python之路-Day05字符串格式化

format格式化方法

根据索引去传一个值

tpl = "my name is {1},age{1}".format("ZhouJieLun",18,"milktea")
print(tpl)

根据键值对的方法去传一个值

如果需要传字典那么在括号的name前面加两个**就可以了
tpl = "my name is {name},age{age}".format(name ="ZhouJieLun",age = 18)
print(tpl)

:s和:d指的是只能传字符串和整数类型

传列表可以是使用一个*的方式
tpl = "my name is {:s},age{:d}".format("ZhouJieLun",18)
print(tpl)

猜你喜欢

转载自blog.csdn.net/ztwsxgz/article/details/82886999