在Python中打印多个参数

use*_*351 283 python printing arguments python-3.x

这只是我的代码片段:

print("Total score for %s is %s  ", name, score)
Run Code Online (Sandbox Code Playgroud)

但我希望它打印出来:

"(姓名)总分为(得分)"

where name是列表中的变量,score是一个整数.这是Python 3.3,如果这有帮助的话.

Ble*_*der 512

有很多方法可以做到这一点.要使用%-formatting 修复当前代码,您需要传入一个元组:

  1. 将其作为元组传递:

    print("Total score for %s is %s" % (name, score))
    
    Run Code Online (Sandbox Code Playgroud)

具有单个元素的元组看起来像('this',).

以下是一些其他常见的方法:

  1. 将其作为字典传递:

    print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
    
    Run Code Online (Sandbox Code Playgroud)

还有新式的字符串格式,可能更容易阅读:

  1. 使用新式字符串格式:

    print("Total score for {} is {}".format(name, score))
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用带数字的新式字符串格式(对于多次重新排序或打印相同的一行很有用):

    print("Total score for {0} is {1}".format(name, score))
    
    Run Code Online (Sandbox Code Playgroud)
  3. 使用具有显式名称的新式字符串格式:

    print("Total score for {n} is {s}".format(n=name, s=score))
    
    Run Code Online (Sandbox Code Playgroud)
  4. 连接字符串:

    print("Total score for " + str(name) + " is " + str(score))
    
    Run Code Online (Sandbox Code Playgroud)

在我看来,最清楚的两个:

  1. 只需将值作为参数传递:

    print("Total score for", name, "is", score)
    
    Run Code Online (Sandbox Code Playgroud)

    如果您不希望print在上面的示例中自动插入空格,请更改sep参数:

    print("Total score for ", name, " is ", score, sep='')
    
    Run Code Online (Sandbox Code Playgroud)

    如果您使用的是Python 2,则无法使用最后两个,因为print它不是Python 2中的函数.但是,您可以从__future__以下位置导入此行为:

    from __future__ import print_function
    
    Run Code Online (Sandbox Code Playgroud)
  2. f在Python 3.6中使用新的-string格式:

    print(f'Total score for {name} is {score}')
    
    Run Code Online (Sandbox Code Playgroud)

  • 当然,总会有一个古老的不赞成的方法:`print("+ str(name)"的总得分+是"+ str(得分))" (7认同)
  • @SnakesandCoffee:我只做`print("总得分为",名字,"是",得分) (5认同)
  • 仅供参考,从Python 3.6开始,我们得到[f-strings](https://www.python.org/dev/peps/pep-0498/),所以你现在也可以`print(f"{总分] name}是{score}")`没有明确的函数调用(只要`name`和`score`显然在范围内). (5认同)
  • 我的+1.这些天我更喜欢`.format()`比旧的'%(元组)`更具可读性 - 尽管我已经看到测试表明`%`插值更快.`print('xxx',a,'yyy',b)`对于简单的情况也很好.我还建议学习`.format_map()`用字典作为参数,用'ssss {key1} xxx {key2}'` - 很好用于从模板生成文本.还有较旧的`string_template%dictionary`.但模板看起来并不干净:`'ssss%(key1)s xxx%(key2)s'`. (4认同)

Vik*_*pta 51

有很多方法可以打印出来.

让我们看看另一个例子.

a = 10
b = 20
c = a + b

#Normal string concatenation
print("sum of", a , "and" , b , "is" , c) 

#convert variable into str
print("sum of " + str(a) + " and " + str(b) + " is " + str(c)) 

# if you want to print in tuple way
print("Sum of %s and %s is %s: " %(a,b,c))  

#New style string formatting
print("sum of {} and {} is {}".format(a,b,c)) 

#in case you want to use repr()
print("sum of " + repr(a) + " and " + repr(b) + " is " + repr(c))

EDIT :

#New f-string formatting from Python 3.6:
print(f'Sum of {a} and {b} is {c}')
Run Code Online (Sandbox Code Playgroud)

  • `print("{0}和{1}的总和是{2}".格式(a,b,c))`是过度杀戮,你可以省略`print("{}的总和而{}是{} ".format(a,b,c))`除非你想改变秩序. (3认同)

Gav*_*hen 30

使用.format()::

print("Total score for {0} is {1}".format(name, score))
Run Code Online (Sandbox Code Playgroud)

要么:

// Recommended, more readable code

print("Total score for {n} is {s}".format(n=name, s=score))
Run Code Online (Sandbox Code Playgroud)

要么:

print("Total score for" + name + " is " + score)
Run Code Online (Sandbox Code Playgroud)

要么:

`print("Total score for %s is %d" % (name, score))`
Run Code Online (Sandbox Code Playgroud)


Abh*_*hek 20

在Python 3.6中,f-string更清晰.

在早期版本中:

print("Total score for %s is %s. " % (name, score))
Run Code Online (Sandbox Code Playgroud)

在Python 3.6中:

print(f'Total score for {name} is {score}.')
Run Code Online (Sandbox Code Playgroud)

会做.

它更高效,更优雅.


Pao*_*lli 14

保持简单,我个人喜欢字符串连接:

print("Total score for " + name + " is " + score)
Run Code Online (Sandbox Code Playgroud)

它适用于Python 2.7和3.X.

注意:如果得分是int,则应将其转换为str:

print("Total score for " + name + " is " + str(score))
Run Code Online (Sandbox Code Playgroud)


sar*_*ora 12

试一试:

print("Total score for", name, "is", score)
Run Code Online (Sandbox Code Playgroud)


The*_*ist 9

按照这个

idiot_type = "the biggest idiot"
year = 22
print("I have been {} for {} years ".format(idiot_type, years))
Run Code Online (Sandbox Code Playgroud)

要么

idiot_type = "the biggest idiot"
year = 22
print("I have been %s for %s years."% (idiot_type, year))
Run Code Online (Sandbox Code Playgroud)

并忘记所有其他人,否则大脑将无法映射所有格式.


小智 6

print("Total score for %s is %s  " % (name, score))
Run Code Online (Sandbox Code Playgroud)

%s可以用%d或代替%f


M.I*_*nat 6

使用f-string

print(f'Total score for {name} is {score}')
Run Code Online (Sandbox Code Playgroud)

或者

使用.format

print("Total score for {} is {}".format(name, score))
Run Code Online (Sandbox Code Playgroud)


Sup*_*bat 5

如果score是数字,那么

print("Total score for %s is %d" % (name, score))
Run Code Online (Sandbox Code Playgroud)

如果得分是一个字符串,那么

print("Total score for %s is %s" % (name, score))
Run Code Online (Sandbox Code Playgroud)

如果得分是一个数字,那么它是%d,如果它是一个字符串,那么它是%s,如果得分是一个浮点数,那么它是%f