len函数python

Python len() function returns the length of the object. Generally, len() function is used with sequences (string, tuple) and collections (dict, set) to get the number of items.

Python len()函数返回对象的长度。 通常,len()函数与序列(字符串, 元组 )和集合( dict , set )一起使用以获取项数。

Python len()函数 (Python len() function)

Python len() function returns the length of the object. This function internally calls __len__() function of the object. So we can use len() function with any object that defines __len__() function.

Python len()函数返回对象的长度。 此函数在内部调用对象的__len __()函数。 因此,我们可以将len()函数与任何定义__len __()函数的对象一起使用。

Let’s look at some examples of using len() function with built-in sequences and collection objects.

让我们看一些将len()函数与内置序列和集合对象一起使用的示例。

# len with sequence
print('string length =', len('abc'))  # string
print('tuple length =', len((1, 2, 3)))  # tuple
print('list length =', len([1, 2, 3, 4]))  # list
print('bytes length =', len(bytes('abc', 'utf-8')))  # bytes
print('range length =', len(range(10, 20, 2)))  # range# len with collections
print('dict length =', len({"a": 1, "b": 2}))  # dict
print('set length =', len(set([1, 2, 3, 3])))  # set
print('frozenset length =', len(frozenset([1, 2, 2, 3])))  # frozenset

Output:

输出:

string length = 3
tuple length = 3
list length = 4
bytes length = 3
range length = 5
dict length = 2
set length = 3
frozenset length = 3

Python len()对象 (Python len() Object)

Let’s define a custom class with __len__() function and call len() function with it’s object as argument.

让我们使用__len __()函数定义一个自定义类,并以其对象作为参数调用len()函数。

class Employee:name = ''def __init__(self, n):self.name = ndef __len__(self):return len(self.name)e = Employee('Pankaj')print('employee object length =', len(e))

Output:

输出:

employee object length = 6

If we remove __len__() function from Employee object, we will get the following exception.

如果我们从Employee对象中删除__len __()函数,我们将得到以下异常。

TypeError: object of type 'Employee' has no len()
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22942/python-len-function

len函数python

len函数python_Python len()函数相关推荐

  1. math ceil函数python_Python ceil函数

    Python ceil函数 最后更新于:2020-03-29 09:57:36 python ceil 内置函数和 int 函数有点类似,计算的结果都是得到一个整数,向上取整: 一.ceil函数简介 ...

  2. update函数python_python update函数

    在前一篇文章  python ChainMap中我们介绍了关于python内置函数 ChainMap的使用,ChainMap函数和update函数类似,都是对字典操作,也是将多个字典合并,那么问题来了 ...

  3. 列表函数python_python 列表函数

    list函数: 功能:将字符创转化为列表,例: 列表基本函数: 1.元素赋值,例: 注意:通过list[0]= 'hel',如果原来位置上有值,会覆盖掉原来的. 2.分片操作 1)显示序列,例: 注意 ...

  4. cycle函数python_Python执行函数的周期实现

    如下python代码是关于Python执行函数的周期实现,需要使用time模块及sched和os模块方法. 首先导入方法模块 #coding=utf-8 import time,sched,os 初始 ...

  5. 分割函数python_python strip() 函数和 split() 函数的详解及实例

    一直以来都分不清楚strip和split的功能,实际上strip是删除的意思:而split则是分割的意思.因此也表示了这两个功能是完全不一样的,strip可以删除字符串的某些字符,而split则是根据 ...

  6. pow函数python_python pow函数怎么用

    python中的pow函数的功能是计算x的y次幂.本篇文章将带大家一起了解一下,pow()函数在Python中的用法.感兴趣的朋友了解一下. 以下是 math 模块 pow() 方法的语法:impor ...

  7. fmod函数python_python fmod函数_Python numpy.fmod方法代码示例

    本文整理汇总了Python中numpy.fmod方法的典型用法代码示例.如果您正苦于以下问题:Python numpy.fmod方法的具体用法?Python numpy.fmod怎么用?Python ...

  8. 查找字符串末尾含关键字_EXCEL函数公式大全之利用FIND函数和RIGHT函数LEN函数取末尾字符...

    EXCEL函数公式大全之利用FIND函数和RIGHT函数LEN函数从字符串末尾取出指定长度的字符,从字符串中取出销售员的姓名.EXCEL函数与公式在工作中使用非常的频繁,会不会使用公式直接决定了我们的 ...

  9. python中len和range函数_for循环len函数和range函数的运用

    函数:len() 作用:返回字符串.列表.字典.元组等长度 语法:len(str) 参数: str:要计算的字符串.列表.字典.元组等 返回值:字符串.列表.字典.元组等元素的长度 实例 1.计算字符 ...

最新文章

  1. 状态保持中的cookie
  2. python3 乱序函数 shuffle 简介
  3. ios10申请权限以及弹出允许使用数据框
  4. 怎么让图片一直转圈_鼠标为什么一直在转圈?如何强制关闭程序?
  5. java内存优化详解_jvm堆内存优化详解
  6. 光影的魔法!Cocos Creator 实现屏幕空间的环境光遮蔽(SSAO)
  7. 如何赋值hook定义的变量
  8. redis rua解决库存问题_【150期】面试官:Redis的各项功能解决了哪些问题?
  9. Base64编码解码
  10. 大数据学习笔记26:MR案例——双重排序(先按月份升序,再按利润降序)
  11. The single product is priced at about 1350 yuan
  12. php7异常抛出的好处,PHP7的错误与异常
  13. vue 饿了么ui如何修改内联样式:element.style
  14. 基于Material Studio软件使用第一性原理预测AlAs的晶格参数
  15. 2021年最新版裁判文书逆向
  16. mysql通过股票代码查数据_如何在交易数据中查询各个版本交易量前三的股票?(MySQL分组排名)...
  17. 什么牌子的蓝牙耳机好?重低音分体式蓝牙耳机!
  18. 董树义 近代微波测量技术_潘时龙:28岁推开微波光子学大门
  19. 金华职业技术学院计算机教研室主任,机械技术系主任及教师赴金华职业技术学院走访调研...
  20. Vim 分屏功能+无插件Vim编程技巧

热门文章

  1. Linux系统安全加固设置详细教程
  2. 筑梦前行 向新而生 华云数据西北区域公司正式乔迁
  3. 网易的猪场有多豪?网友:请你低调一点
  4. 微信公众平台开发(49)物联网硬件设备控制技术
  5. vue中如何清除echarts上次保留的数据(亲测有效)
  6. input type=button与asp:button的区别,以及runat=server的作用
  7. 虚拟带库 Vistor + TSM 安装 (在家折腾了一个周末)
  8. CDH/HDP迁移之路
  9. Docker的深入浅出(入门新手篇)
  10. MybatisPlus实现多条件拼接动态查询