[英]How can I print multiple things (fixed text and/or variable values) on the same line?
這只是我的代碼片段:
print("Total score for %s is %s ", name, score)
但我希望它打印出來:
“(姓名)的總分是(分數)”
其中name
是列表中的變量, score
是 integer。如果有幫助的話,這是 Python 3.3。
有很多方法可以做到這一點。 要使用%
-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
字符串格式:
print(f'Total score for {name} is {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}')
使用: .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.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}')
在 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))
忘記所有其他人,否則大腦將無法映射所有格式。
你試一試:
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))
如果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
。
最簡單的方法如下
print(f"Total score for {name} is {score}")
只需在前面加一個“f”。
這可能是casting issue
。 當您嘗試組合兩種不同types of variables
時,就會發生Casting syntax
。 由於我們不能總是將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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.