在Python中,len()函数用于返回指定对象的长度或者元素个数。它可以接受字符串、列表、元组、集合、字典等对象作为参数,并返回它们的长度。例如:
# 字符串长度s = "hello"print(len(s)) # 输出 5# 列表长度lst = [1, 2, 3, 4, 5]print(len(lst)) # 输出 5# 元组长度tup = (1, 2, 3)print(len(tup)) # 输出 3# 集合长度set1 = {1, 2, 3, 4, 5}print(len(set1)) # 输出 5# 字典长度dic = {'a': 1, 'b': 2, 'c': 3}print(len(dic)) # 输出 3总之,len()函数可以用于计算各种对象的长度,从而方便地获取它们的元素个数。


