Python:总和字符串长度
发布于 2021-01-29 18:02:03
与使用循环相比,在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]
关注者
0
被浏览
48
1 个回答