use*_*351 283 python printing arguments python-3.x
这只是我的代码片段:
print("Total score for %s is %s ", name, score)
Run Code Online (Sandbox Code Playgroud)
但我希望它打印出来:
"(姓名)总分为(得分)"
where name是列表中的变量,score是一个整数.这是Python 3.3,如果这有帮助的话.
Ble*_*der 512
有很多方法可以做到这一点.要使用%-formatting 修复当前代码,您需要传入一个元组:
将其作为元组传递:
print("Total score for %s is %s" % (name, score))
Run Code Online (Sandbox Code Playgroud)具有单个元素的元组看起来像('this',).
以下是一些其他常见的方法:
将其作为字典传递:
print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
Run Code Online (Sandbox Code Playgroud)还有新式的字符串格式,可能更容易阅读:
使用新式字符串格式:
print("Total score for {} is {}".format(name, score))
Run Code Online (Sandbox Code Playgroud)使用带数字的新式字符串格式(对于多次重新排序或打印相同的一行很有用):
print("Total score for {0} is {1}".format(name, score))
Run Code Online (Sandbox Code Playgroud)使用具有显式名称的新式字符串格式:
print("Total score for {n} is {s}".format(n=name, s=score))
Run Code Online (Sandbox Code Playgroud)连接字符串:
print("Total score for " + str(name) + " is " + str(score))
Run Code Online (Sandbox Code Playgroud)在我看来,最清楚的两个:
只需将值作为参数传递:
print("Total score for", name, "is", score)
Run Code Online (Sandbox Code Playgroud)
如果您不希望print在上面的示例中自动插入空格,请更改sep参数:
print("Total score for ", name, " is ", score, sep='')
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Python 2,则无法使用最后两个,因为print它不是Python 2中的函数.但是,您可以从__future__以下位置导入此行为:
from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)f在Python 3.6中使用新的-string格式:
print(f'Total score for {name} is {score}')
Run Code Online (Sandbox Code Playgroud)Vik*_*pta 51
有很多方法可以打印出来.
让我们看看另一个例子.
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}')
Run Code Online (Sandbox Code Playgroud)
Gav*_*hen 30
使用.format()::
print("Total score for {0} is {1}".format(name, score))
Run Code Online (Sandbox Code Playgroud)
要么:
// Recommended, more readable code
print("Total score for {n} is {s}".format(n=name, s=score))
Run Code Online (Sandbox Code Playgroud)
要么:
print("Total score for" + name + " is " + score)
Run Code Online (Sandbox Code Playgroud)
要么:
`print("Total score for %s is %d" % (name, score))`
Run Code Online (Sandbox Code Playgroud)
Abh*_*hek 20
在Python 3.6中,f-string更清晰.
在早期版本中:
print("Total score for %s is %s. " % (name, score))
Run Code Online (Sandbox Code Playgroud)
在Python 3.6中:
print(f'Total score for {name} is {score}.')
Run Code Online (Sandbox Code Playgroud)
会做.
它更高效,更优雅.
Pao*_*lli 14
保持简单,我个人喜欢字符串连接:
print("Total score for " + name + " is " + score)
Run Code Online (Sandbox Code Playgroud)
它适用于Python 2.7和3.X.
注意:如果得分是int,则应将其转换为str:
print("Total score for " + name + " is " + str(score))
Run Code Online (Sandbox Code Playgroud)
sar*_*ora 12
试一试:
print("Total score for", name, "is", score)
Run Code Online (Sandbox Code Playgroud)
按照这个
idiot_type = "the biggest idiot"
year = 22
print("I have been {} for {} years ".format(idiot_type, years))
Run Code Online (Sandbox Code Playgroud)
要么
idiot_type = "the biggest idiot"
year = 22
print("I have been %s for %s years."% (idiot_type, year))
Run Code Online (Sandbox Code Playgroud)
并忘记所有其他人,否则大脑将无法映射所有格式.
小智 6
print("Total score for %s is %s " % (name, score))
Run Code Online (Sandbox Code Playgroud)
%s可以用%d或代替%f
使用f-string:
print(f'Total score for {name} is {score}')
Run Code Online (Sandbox Code Playgroud)
或者
使用.format:
print("Total score for {} is {}".format(name, score))
Run Code Online (Sandbox Code Playgroud)
如果score是数字,那么
print("Total score for %s is %d" % (name, score))
Run Code Online (Sandbox Code Playgroud)
如果得分是一个字符串,那么
print("Total score for %s is %s" % (name, score))
Run Code Online (Sandbox Code Playgroud)
如果得分是一个数字,那么它是%d,如果它是一个字符串,那么它是%s,如果得分是一个浮点数,那么它是%f
| 归档时间: |
|
| 查看次数: |
843494 次 |
| 最近记录: |