我有这样的代码:

score = 100
name = 'Alice'
print('Total score for %s is %s', name, score)

我想打印出Alice的总分是100,但是我得到了%s的总分是%s Alice 100。我怎样才能让所有东西都以正确的顺序和正确的格式打印出来?


请参见:如何在同一行上打印多个内容,一次打印一个?; 我如何把一个变量的值在一个字符串(插值到字符串)?


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

将其作为元组传递: print(" %s的总分为%s" %(名称,分数))

只有一个元素的元组看起来像('this',)。

这里有一些其他常见的方法:

将其作为字典传递: 打印(“总分为% (n) s % (s) s % {' n ':名字,“s”:分数})

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

使用新样式的字符串格式化: print("{}的总分为{}"。格式(名称、成绩) 使用带有数字的新型字符串格式(用于重新排序或多次打印同一字符串): 打印(“{0}的总分是{1}”。格式(名称、成绩) 使用带有显式名称的新型字符串格式化: print(“{n}的总分为{s}”。格式(n =名字,s =分数)) 连接字符串: 打印(“+ str(名称)+”的总分为“+ str(分数)”)

在我看来,最明确的两点是:

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


如果分数是一个数字,那么

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

OR

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

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

或者:Python 3.6中的f-string格式:

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

Or

使用.format:

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

这可能是选角的问题。强制转换语法发生在试图组合两种不同类型的变量时。由于不能将字符串转换为整数或始终为浮点数,所以必须将整数转换为字符串。这就是你要做的。: str (x)。要转换为整数,它是:int(x),浮点数是float(x)。我们的代码将是:

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

也!运行此代码片段以查看如何转换不同类型变量的表格!

<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”就行了。