问题:在Python中打印多个参数

这只是我的代码的一部分:

print("Total score for %s is %s  ", name, score)

但我希望它打印出来:

“(姓名)的总分是(分数)”

其中name是列表中的变量,score是整数。如果有帮助的话,这就是Python 3.3。

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 name is a variable in a list and score is an integer. This is Python 3.3 if that helps at all.


回答 0

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

  1. 将其作为元组传递:

    print("Total score for %s is %s" % (name, score))

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

这是其他一些常见的实现方法:

  1. 将其作为字典传递:

    print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})

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

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

    print("Total score for {} is {}".format(name, score))
  2. 使用带有数字的新型字符串格式(可用于重新排序或多次打印相同的字符):

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

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

    print("Total score for " + str(name) + " is " + str(score))

我认为最清楚的两个是:

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

    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_function
  2. f在Python 3.6中使用新的-string格式:

    print(f'Total score for {name} is {score}')

There are many ways to do this. To fix your current code using %-formatting, you need to pass in a tuple:

  1. Pass it as a tuple:

    print("Total score for %s is %s" % (name, score))
    

A tuple with a single element looks like ('this',).

Here are some other common ways of doing it:

  1. Pass it as a dictionary:

    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:

  1. Use new-style string formatting:

    print("Total score for {} is {}".format(name, score))
    
  2. 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))
    
  3. Use new-style string formatting with explicit names:

    print("Total score for {n} is {s}".format(n=name, s=score))
    
  4. Concatenate strings:

    print("Total score for " + str(name) + " is " + str(score))
    

The clearest two, in my opinion:

  1. Just pass the values as parameters:

    print("Total score for", name, "is", score)
    

    If you don’t want spaces to be inserted automatically by print in the above example, change the sep parameter:

    print("Total score for ", name, " is ", score, sep='')
    

    If you’re using Python 2, won’t be able to use the last two because print isn’t a function in Python 2. You can, however, import this behavior from __future__:

    from __future__ import print_function
    
  2. Use the new f-string formatting in Python 3.6:

    print(f'Total score for {name} is {score}')
    

回答 1

有很多打印方法。

让我们看另一个例子。

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}')

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}')

回答 2

使用方法.format()

print("Total score for {0} is {1}".format(name, score))

要么:

// Recommended, more readable code

print("Total score for {n} is {s}".format(n=name, s=score))

要么:

print("Total score for" + name + " is " + score)

要么:

`print("Total score for %s is %d" % (name, score))`

Use: .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))`

回答 3

在Python 3.6中,f-string它更加干净。

在早期版本中:

print("Total score for %s is %s. " % (name, score))

在Python 3.6中:

print(f'Total score for {name} is {score}.')

会做。

它更高效,更优雅。

In Python 3.6, f-string is much cleaner.

In earlier version:

print("Total score for %s is %s. " % (name, score))

In Python 3.6:

print(f'Total score for {name} is {score}.')

will do.

It is more efficient and elegant.


回答 4

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

print("Total score for " + name + " is " + score)

它同时适用于Python 2.7和3.X。

注意:如果score是一个int,则应将其转换为str

print("Total score for " + name + " is " + str(score))

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.

NOTE: If score is an int, then, you should convert it to str:

print("Total score for " + name + " is " + str(score))

回答 5

你试一试:

print("Total score for", name, "is", score)

Just try:

print("Total score for", name, "is", score)

回答 6

只要遵循这个

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

要么

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

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

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.


回答 7

print("Total score for %s is %s  " % (name, score))

%s可以替换为%d%f

print("Total score for %s is %s  " % (name, score))

%s can be replace by %d or %f


回答 8

用途f-string

print(f'Total score for {name} is {score}')

要么

用途.format

print("Total score for {} is {}".format(name, score))

Use f-string:

print(f'Total score for {name} is {score}')

Or

Use .format:

print("Total score for {} is {}".format(name, score))

回答 9

如果score是数字,则

print("Total score for %s is %d" % (name, score))

如果score是一个字符串,则

print("Total score for %s is %s" % (name, score))

如果score是数字,%d则为,如果是字符串%s,则为,如果score是浮点型,则为%f

If score is a number, then

print("Total score for %s is %d" % (name, score))

If score is a string, then

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


回答 10

这是我的工作:

print("Total score for " + name + " is " + score)

请记住在for前后放置一个空格is

This is what I do:

print("Total score for " + name + " is " + score)

Remember to put a space after for and before and after is.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。