在 Python 中是否有比使用循环更惯用的方法来求和字符串长度?
length = 0
for string in strings:
length += len(string)
我试过 sum()
,但它只适用于整数:
>>> sum('abc', 'de')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]
原文由 Zaz 发布,翻译遵循 CC BY-SA 4.0 许可协议
我知道这是一个老问题,但我不禁注意到 Python 错误消息 告诉 您如何执行此操作:
所以: