python带变量的字符串输出python format 格式化函数
主要适⽤于带变量的字符串输出
1. ⽅法⼀
strs ={"you":"hello","me":"world"}
print('i want to say {you} {me}'.format(**strs)) # strs 必须为⼀个字典
输出:
i want to say hello world
2. ⽅法⼆
print('i want to say {you} {me}'.format(you ='hello',me ='world')) # 可以直接赋值
输出:
i want to say hello world
3. ⽅法三
print('i want to say {} {}'.format('hello','world'))
输出:
i want to say hello world
4. 循环输出
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
python格式化输出formati want to say 2 4
i want to say 3 6
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论