2012-05-09 89 views

回答

77
answer = True 
myvar = "the answer is " + str(answer) 

Python沒有做隱式轉換,因爲隱式轉換可能會掩蓋重要的邏輯錯誤。只投答案的一個字符串本身,以獲取其字符串表示(「真」),或使用字符串格式化像這樣:

myvar = "the answer is %s" % answer 

注意這個問題的答案必須設置爲True(大小寫很重要)。

7
answer = True 
myvar = "the answer is " + str(answer) 

myvar = "the answer is %s" % answer 
+0

報價之外的'%s'不應該存在,但是這確實是正確的。 – Makoto

+0

糟糕,修復了錯字 – Squazic

9

推薦的方法是讓str.format處理鑄件(docs)。使用%s替代的方法最終可能會被棄用(請參閱PEP3101)。

>>> answer = True 
>>> myvar = "the answer is {}".format(answer) 
>>> print myvar 
the answer is True