python format 格式化函数
主要适用于带变量的字符串输出
- 方法一
strs = {"you":"hello","me":"world"}
print('i want to say {you} {me}'.format(**strs)) # strs 必须为一个字典
输出:
i want to say hello world
- 方法二
print('i want to say {you} {me}'.format(you = 'hello',me = 'world')) # 可以直接赋值
输出:
i want to say hello world
- 方法三
print('i want to say {} {}'.format('hello','world'))
输出:
i want to say hello world
- 循环输出
for i in range(4):
print("i want to say {} {}".format(i,i*2))
输出:
i want to say 0 0
i want to say 1 2
i want to say 2 4
i want to say 3 6