【发布时间】:2013-02-23 13:10:53
【问题描述】:
这只是我的代码的一个 sn-p:
print("Total score for %s is %s ", name, score)
但我希望它打印出来:
“(姓名)的总分是(分数)”
其中name 是列表中的变量,score 是整数。如果有帮助的话,这就是 Python 3.3。
【问题讨论】:
标签: python printing python-3.x arguments
这只是我的代码的一个 sn-p:
print("Total score for %s is %s ", name, score)
但我希望它打印出来:
“(姓名)的总分是(分数)”
其中name 是列表中的变量,score 是整数。如果有帮助的话,这就是 Python 3.3。
【问题讨论】:
标签: python printing python-3.x arguments
有很多方法可以做到这一点。要使用%-formatting 修复您当前的代码,您需要传入一个元组:
将其作为元组传递:
print("Total score for %s is %s" % (name, score))
具有单个元素的元组看起来像 ('this',)。
这里有一些其他常见的方法:
将其作为字典传递:
print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
还有新式的字符串格式,可能更容易阅读:
使用新式字符串格式:
print("Total score for {} is {}".format(name, score))
使用带有数字的新型字符串格式(对于重新排序或多次打印相同的字符串很有用):
print("Total score for {0} is {1}".format(name, score))
使用带有明确名称的新型字符串格式:
print("Total score for {n} is {s}".format(n=name, s=score))
连接字符串:
print("Total score for " + str(name) + " is " + str(score))
我认为最清晰的两个:
只需将值作为参数传递:
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
在 Python 3.6 中使用新的f-string 格式:
print(f'Total score for {name} is {score}')
【讨论】:
print("Total score for "+str(name)"+ is "+str(score))
print("Total score for", name, "is", score)
.format(),因为它比旧的% (tuple) 更具可读性——即使我已经看到测试表明% 插值更快。 print('xxx', a, 'yyy', b) 也适用于简单的情况。我还建议以字典为参数学习.format_map(),并以'ssss {key1} xxx {key2}' 学习——非常适合从模板生成文本。还有较旧的string_template % dictionary。但模板看起来并不那么干净:'ssss %(key1)s xxx %(key2)s'.
print(f"Total score for {name} is {score}")(只要 name 和 score 显然在范围内)。
print("Total score for "+str(name)"+ is "+str(score)) 未获批准?
有很多方法可以打印出来。
让我们看看另一个例子。
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}')
【讨论】:
#Normal string concatenation) 至少具有误导性。
使用:.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}')
【讨论】:
在 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)
它适用于 Python 2.7 和 3.X。
注意:如果 score 是一个 int,那么,你应该把它转换成 str:
print("Total score for " + name + " is " + str(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))
忘记所有其他的,否则大脑将无法映射所有格式。
【讨论】:
f"I have been {a} for {b} years" 呢?我最近只去那个...
试试吧:
print("Total score for", name, "is", score)
【讨论】:
print("Total score for %s is %s " % (name, score))
%s 可以替换为%d 或%f
【讨论】:
使用f-string:
print(f'Total score for {name} is {score}')
或者
使用.format:
print("Total score for {} is {}".format(name, score))
【讨论】:
.format?
"和'报价?
如果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
【讨论】:
这就是我的工作:
print("Total score for " + name + " is " + score)
记得在for 之后和is 之前和之后放置一个空格。
【讨论】:
这可能是casting issue。 Casting syntax 发生在您尝试组合两个不同的 types of variables 时。由于我们无法始终将string 转换为integer 或float,因此我们必须将integers 转换为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>
【讨论】: