Python len函数

时间:2025-02-11 09:19:10

描述:

返回对象的长度(项目数)参数可以是序列(例如字符串、字节、元组、列表或范围)或集合(例如字典、集合或冻结集合)。

语法:

len(s)

参数介绍:

s --- 对象

返回值:

返回对象的长度

下面例子展示len()函数使用方法

a = (1,2,3,4,5,6) #元组
b = [1,2,3,4] #列表
c = range(0,11) #range
d = {'name':'lisi','age':14} #字典
e = 'helloworld' #字符串
f = set([1,2,3,4,5]) #集合
g = frozenset([1,2,3,4,5,8]) #冻结集合

print(len(a))
print(len(b))
print(len(c))
print(len(d))
print(len(e))
print(len(f))
print(len(g))

输出

6
4
11
2
10
5
6

本期len()函数就学到这里。