363

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.

4

13 回答 13

647

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}')
    
于 2013-03-08T03:52:57.977 回答
63

有很多方法可以打印出来。

让我们看另一个例子。

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}')
于 2016-08-11T13:07:38.753 回答
53

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

或者: 从Python 3.6f-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}') 
于 2018-01-18T11:23:38.133 回答
22

在 Python 3.6f-string中,干净得多。

在早期版本中:

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

在 Python 3.6 中:

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

会做。

它更高效、更优雅。

于 2017-05-23T10:09:21.190 回答
15

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

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

它适用于 Python 2.7 和 3.X。

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

print("Total score for " + name + " is " + str(score))
于 2015-04-01T20:57:55.560 回答
14

只要按照这个

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

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

于 2017-09-22T07:11:56.847 回答
12

试试看嘛:

print("Total score for", name, "is", score)
于 2014-07-30T05:00:47.167 回答
7

使用f-string

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

或者

使用.format

print("Total score for {} is {}".format(name, score))
于 2018-07-07T06:08:14.197 回答
6
print("Total score for %s is %s  " % (name, score))

%s可以替换为%d%f

于 2016-03-03T16:51:36.773 回答
5

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

于 2016-07-11T19:53:48.190 回答
3

这就是我所做的:

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

记得在for前后加一个空格is

于 2015-12-21T09:51:44.343 回答
0

最简单的方法如下

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

只需在前面加上一个“f”。

于 2022-02-14T16:35:05.883 回答
0

这可能是一个casting issue. Casting syntax当您尝试将两个不同的types of variables. 由于我们不能将 a 转换stringintegeror floatalways,我们必须将 our 转换integers为 a string。这就是你的做法。:str(x)。要转换为整数,它是:int(x),浮点数是float(x). 我们的代码将是:

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>

于 2021-01-01T21:57:18.313 回答