python print用法多个参数_在Python中打印多个参数

5333a207000118af02200220-100-100.jpg

宝慕林4294392

有很多方法可以做到这一点。要使用%-formatting 修复当前代码,您需要传入一个元组:将其作为元组传递:print("Total score for %s is %s" % (name, score))具有单个元素的元组看起来像('this',)。这是其他一些常见的实现方法:将其作为字典传递:print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})还有一种新型的字符串格式,可能更容易阅读:使用新型的字符串格式:print("Total score for {} is {}".format(name, score))使用带有数字的新型字符串格式(可用于重新排序或多次打印相同的字符):print("Total score for {0} is {1}".format(name, score))使用带有显式名称的新型字符串格式:print("Total score for {n} is {s}".format(n=name, s=score))连接字符串:print("Total score for " + str(name) + " is " + str(score))我认为最清楚的两个是:只需将值作为参数传递:print("Total score for", name, "is", score)如果您不希望print在上面的示例中自动插入空格,请更改sep参数:print("Total score for ", name, " is ", score, sep='')如果您使用的是Python 2,将不能使用最后两个,因为print这不是Python 2中的函数。不过,您可以从__future__以下方式导入此行为:from __future__ import print_functionf在Python 3.6中使用新的-string格式:print(f'Total score for {name} is {score}')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值