0
点赞
收藏
分享

微信扫一扫

Python的len()函数

芷兮离离 2022-04-29 阅读 88
python

len()函数

1:作用:返回字符串、列表、字典、元组等长度

2:语法:len(str)

3:参数:str:要计算的字符串、列表、字典、元组等

4:返回值:字符串、列表、字典、元组等元素的长度

5:实例5.1、计算字符串的长度:

>>> str = "hello python"

>>> len(str)

12

5.2、计算列表的元素个数:

>>> list = ['h','e','l','l','o']

>>> len(list)

5

5.3、计算字典的总长度(即键值对总数):

>>> dic = {'num':1,'name':"python"}

>>> len(dic)

2

5.4、计算元组元素个数:

>>> tuple=('p','y','t','h','o','n')

>>> len(tuple)

6

举报

相关推荐

0 条评论