`len` 函数在 Python 中是一个内置函数,用于返回一个对象中元素的个数。它可以应用于多种数据类型,包括字符串、列表、元组、字典和集合。
len(object)
其中 `object` 是要计算长度的对象。
例如:
字符串长度
string = "Hello World"
print(len(string)) 输出:11
列表长度
list_example = [1, 2, 3, 4, 5]
print(len(list_example)) 输出:5
元组长度
tuple_example = (1, 2, 3, 4, 5)
print(len(tuple_example)) 输出:5
字典长度(键值对的数量)
dict_example = {'a': 1, 'b': 2, 'c': 3}
print(len(dict_example)) 输出:3
集合长度
set_example = {1, 2, 3, 4, 5}
print(len(set_example)) 输出:5
`len` 函数返回的是对象中元素的个数,而不是字节数或其他属性