在 Python 中打印多个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15286401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print multiple arguments in Python
提问by user1985351
This is just a snippet of my code:
这只是我的代码片段:
print("Total score for %s is %s ", name, score)
But I want it to print out:
但我希望它打印出来:
"Total score for (name) is (score)"
“(姓名)的总分是(分数)”
where nameis a variable in a list and scoreis an integer. This is Python 3.3 if that helps at all.
其中name是列表中的变量,score是整数。如果这有帮助的话,这就是 Python 3.3。
回答by Blender
There are many ways to do this. To fix your current code using %-formatting, you need to pass in a tuple:
有很多方法可以做到这一点。要使用%-formatting修复当前代码,您需要传入一个元组:
Pass it as a tuple:
print("Total score for %s is %s" % (name, score))
将其作为元组传递:
print("Total score for %s is %s" % (name, score))
A tuple with a single element looks like ('this',).
具有单个元素的元组看起来像('this',).
Here are some other common ways of doing it:
以下是其他一些常见的方法:
Pass it as a dictionary:
print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
将其作为字典传递:
print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
There's also new-style string formatting, which might be a little easier to read:
还有新样式的字符串格式,这可能更容易阅读:
Use new-style string formatting:
print("Total score for {} is {}".format(name, score))Use new-style string formatting with numbers (useful for reordering or printing the same one multiple times):
print("Total score for {0} is {1}".format(name, score))Use new-style string formatting with explicit names:
print("Total score for {n} is {s}".format(n=name, s=score))Concatenate strings:
print("Total score for " + str(name) + " is " + str(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))
The clearest two, in my opinion:
在我看来,最清楚的两个:
Just pass the values as parameters:
print("Total score for", name, "is", score)If you don't want spaces to be inserted automatically by
printin the above example, change thesepparameter:print("Total score for ", name, " is ", score, sep='')If you're using Python 2, won't be able to use the last two because
printisn't a function in Python 2. You can, however, import this behavior from__future__:from __future__ import print_functionUse the new
f-string formatting in Python 3.6:print(f'Total score for {name} is {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}')
回答by sarora
Just try:
你试一试:
print("Total score for", name, "is", score)
回答by Paolo Rovelli
Keeping it simple, I personally like string concatenation:
保持简单,我个人喜欢字符串连接:
print("Total score for " + name + " is " + score)
It works with both Python 2.7 an 3.X.
它适用于 Python 2.7 和 3.X。
NOTE: If score is an int, then, you should convert it to str:
注意:如果 score 是int,则应将其转换为str:
print("Total score for " + name + " is " + str(score))
回答by Paolo Rovelli
This is what I do:
这就是我所做的:
print("Total score for " + name + " is " + score)
Remember to put a space after forand before and after is.
请记住在for前后各放一个空格is。
回答by user6014455
print("Total score for %s is %s " % (name, score))
%scan be replace by %dor %f
%s可以替换为%d或%f
回答by Supercolbat
If scoreis a number, then
如果score是一个数,那么
print("Total score for %s is %d" % (name, score))
If score is a string, then
如果 score 是一个字符串,那么
print("Total score for %s is %s" % (name, score))
If score is a number, then it's %d, if it's a string, then it's %s, if score is a float, then it's %f
如果 score 是一个数字,那么它就是%d,如果它是一个字符串,那么它就是%s,如果 score 是一个浮点数,那么它就是%f
回答by Vikas Gupta
There are many ways to print that.
有很多方法可以打印出来。
Let's have a look with another example.
让我们再看一个例子。
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}')
回答by Abhishek
In Python 3.6, f-stringis much cleaner.
在 Python 3.6 中,f-string要干净得多。
In earlier version:
在早期版本中:
print("Total score for %s is %s. " % (name, score))
In Python 3.6:
在 Python 3.6 中:
print(f'Total score for {name} is {score}.')
will do.
会做。
It is more efficient and elegant.
它更高效、更优雅。
回答by TheExorcist
Just follow this
只要按照这个
idiot_type = "the biggest idiot"
year = 22
print("I have been {} for {} years ".format(idiot_type, years))
OR
或者
idiot_type = "the biggest idiot"
year = 22
print("I have been %s for %s years."% (idiot_type, year))
And forget all others, else the brain won't be able to map all the formats.
忘记所有其他人,否则大脑将无法映射所有格式。
回答by Gavriel Cohen
Use: .format():
使用:.format():
print("Total score for {0} is {1}".format(name, score))
Or:
或者:
// Recommended, more readable code
print("Total score for {n} is {s}".format(n=name, s=score))
Or:
或者:
print("Total score for" + name + " is " + score)
Or:
或者:
`print("Total score for %s is %d" % (name, score))`

