[英]What is the difference between elements in list between "Quotation mark" and not in Quotation mark in python?
引號中的元素是數據類型“字符串”,而沒有引號,它被視為整數/浮點數(在這種情況下為 int)。 因此,當您使用 sum 時,它僅適用於 int/float 類型。
因此,您會收到字符串類型的錯誤。
雖然您可以“添加”(連接)字符串,但sum
旨在與數值一起使用,為了實現類似的行為,但對於字符串,您可以使用str.join
:
>>> L = ['1', '2', '3', '4', '5']
>>> ''.join(L) # join the strings in `L` using the empty string '' as the connector
'12345'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.