在Python中打印多个参数

这只是我的代码片段:

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

但是我想打印出来:

“(姓名)总分为(分数)”

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

1129930 次浏览

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

  1. 将其作为元组传递:

    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)
    

    如果你不想在上面的例子中通过print自动插入空格,更改sep参数:

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

    如果你正在使用Python 2,将不能使用后两个,因为print不是Python 2中的函数。但是,你可以从__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}')
    

试试:

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

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

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

它适用于Python 2.7和3.X。

注意:如果score是int,那么,你应该将它转换为str:

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

这就是我所做的:

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

记住在for之后和is之前和之后加一个空格。

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

%s可以替换为%d%f

如果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

有很多种打印方法。

让我们看另一个例子。

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

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

会做的事情。

它更加高效和优雅。

跟着这个

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

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

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

用途:.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))

或:f-string格式化Python 3.6:

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

可以使用repr并自动添加'':

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


# or for advanced:
print(f'Total score for {name!r} is {score!r}')

使用f-string:

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

使用.format:

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

这可能是casting issue。当你试图组合两个不同的types of variables时,会发生Casting syntax。由于不能总是将string转换为integerfloat,所以必须将integers转换为string。这就是你要做的。: str(x)。要转换为整数,它是:int(x),而浮点数是Casting syntax0。我们的代码将是:

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

也!运行这个snippet来查看如何转换不同的types of variables!

<table style="border-collapse: collapse; width: 100%;background-color:maroon; color: #00b2b2;">
<tbody>
<tr>
<td style="width: 50%;font-family: serif; padding: 3px;">Booleans</td>
<td style="width: 50%;font-family: serif; padding: 3px;"><code>bool()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding: 3px">Dictionaries</td>
<td style="width: 50%;font-family: serif;padding: 3px"><code>dict()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding: 3px">Floats</td>
<td style="width: 50%;font-family: serif;padding: 3px"><code>float()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding:3px">Integers</td>
<td style="width: 50%;font-family: serif;padding:3px;"><code>int()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding: 3px">Lists</td>
<td style="width: 50%font-family: serif;padding: 3px;"><code>list()</code></td>
</tr>
</tbody>
</table>

最简单的方法如下

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

只要加上"f"在前面。