我有这样的代码:

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

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


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


当前回答

有很多种打印方法。

让我们看另一个例子。

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

其他回答

有很多种打印方法。

让我们看另一个例子。

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

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

有很多方法可以做到这一点。要使用%-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}')

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

会做的事情。

它更加高效和优雅。

这就是我所做的:

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

记住在for和is后面加空格。