这只是我的代码片段:
1print("Total score for %s is %s ", name, score)
但我想把它打印出来:
"Total score for (name) is (score)"
其中name是列表中的变量,score是整数。这是python 3.3,如果这有帮助的话。
有很多方法可以做到这一点。要使用%格式修复当前代码,需要传入一个元组:
将其作为元组传递:
1print("Total score for %s is %s" % (name, score))
一个只有一个元素的元组看起来像('this',)。
以下是一些其他常见的方法:
将其作为字典传递:
1print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
还有新样式的字符串格式,可能更容易阅读:
使用新样式字符串格式:
1print("Total score for {} is {}".format(name, score))
将新样式的字符串格式与数字一起使用(用于对同一字符串进行多次重新排序或打印):
1print("Total score for {0} is {1}".format(name, score))
使用具有显式名称的新样式字符串格式:
1print("Total score for {n} is {s}".format(n=name, s=score))
连接字符串:
1print("Total score for" + str(name) +" is" + str(score))
我认为最清楚的两个:
只需将值作为参数传递:
1print("Total score for", name,"is", score)
如果在上面的示例中不希望print自动插入空格,请更改sep参数:
1print("Total score for", name," is", score, sep='')
如果您使用的是python 2,那么就不能使用最后两个,因为print不是python2中的函数。但是,您可以从__future__导入此行为:
1from __future__ import print_function
在python 3.6中使用新的f字符串格式:
1print(f'Total score for {name} is {score}')
当然,总有一种古老的不被认可的方法:print("Total score for"+str(name)"+ is"+str(score))。
@蛇和咖啡:我只要做一点。
我的1。现在,我更喜欢.format()比旧的% (tuple)更具可读性,尽管我已经看到一些测试显示%插值更快。对于简单的病例,print('xxx', a, 'yyy', b)也可以。我还建议学习.format_map(),使用字典作为参数,使用'ssss {key1} xxx {key2}',很好地从模板生成文本。还有老的string_template % dictionary。但是这些模板看起来不那么干净:'ssss %(key1)s xxx %(key2)s'。
仅供参考,从python 3.6开始,我们得到了f字符串,所以现在您也可以在没有显式函数调用的情况下执行print(f"Total score for {name} is {score}")(只要name和score明显在范围内)。
有很多方法可以打印它。
让我们用另一个例子来看看。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23a = 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}')
print("sum of {0} and {1} is {2}".format(a,b,c))是多余的,你可以省略print("sum of {} and {} is {}".format(a,b,c))除非你想改变订单。
用途:.format():
1print("Total score for {0} is {1}".format(name, score))
或:
1
2
3// Recommended, more readable code
print("Total score for {n} is {s}".format(n=name, s=score))
或:
1print("Total score for" + name +" is" + score)
或:
1`print("Total score for %s is %d" % (name, score))`
在python 3.6中,f-string要干净得多。
在早期版本中:
1print("Total score for %s is %s." % (name, score))
在Python 3.6中:
1print(f'Total score for {name} is {score}.')
会的。
它更高效,更优雅。
保持简单,我个人喜欢字符串连接:
1print("Total score for" + name +" is" + score)
它与Python2.7和3.x都可以使用。
注:如果分数是int,则应将其转换为str:
1print("Total score for" + name +" is" + str(score))
试一试:
1print("Total score for", name,"is", score)
跟着这个走
1
2
3idiot_type ="the biggest idiot"
year = 22
print("I have been {} for {} years".format(idiot_type, years))
或
1
2
3idiot_type ="the biggest idiot"
year = 22
print("I have been %s for %s years."% (idiot_type, year))
忘记所有其他的,否则大脑就无法映射所有的格式。
如果score是一个数字,那么
1print("Total score for %s is %d" % (name, score))
如果分数是一个字符串,那么
1print("Total score for %s is %s" % (name, score))
如果分数是数字,那么它是%d,如果是字符串,那么它是%s,如果分数是浮点数,那么它是%f。
1print("Total score for %s is %s " % (name, score))
%s可由%d或%f代替。
使用f-string:
1print(f'Total score for {name} is {score}')
或
使用.format:
1print("Total score for {} is {}".format(name, score))
我就是这样做的:
1print("Total score for" + name +" is" + score)
记住在for之后和is之前和之后放一个空格。