網站首頁 編程語言 正文
Python中的len()函數
描述:
- 返回對象的長度(項目數)參數可以是序列(例如字符串、字節、元組、列表或范圍)或集合(例如字典、集合或凍結集合)。
語法:
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()函數獲取字符串長度
函數:len()
作用:返回字符串、列表、字典、元組等長度
語法:len(str)
參數:
- str:要計算的字符串、列表、字典、元組等
- 返回值:字符串、列表、字典、元組等元素的長度
實例
- 計算字符串的長度:
>>> s = "hello good boy doiido" >>> len(s) 21
- 計算列表的元素個數:
>>> l = ['h','e','l','l','o'] >>> len(l) 5
- 計算字典的總長度(即鍵值對總數):
>>> d = {'num':123,'name':"doiido"} >>> len(d) 2
- 計算元組元素個數:
>>> t = ('G','o','o','d') >>> len(t) 4
總結
原文鏈接:https://blog.csdn.net/qq_29720657/article/details/102910797
相關推薦
- 2022-11-29 redis的主從復制,哨兵和cluster集群
- 2022-06-06 C++中隊列queue的用法實例詳解_C 語言
- 2021-12-06 Android優化提升應用啟動速度及Splash頁面的設計_Android
- 2022-04-11 Python如何在終端彩色打印輸出_python
- 2022-08-27 Python?Pandas聚合函數的應用示例_python
- 2022-05-06 SQLite3+Qt開發:SQLite3簡要介紹+在Qt5中使用步驟
- 2022-06-08 Python使用PyYAML庫讀寫yaml文件的方法_python
- 2022-07-26 msSQL中having的用處詳解_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支