網站首頁 編程語言 正文
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-28 matplotlib?雙y軸繪制及合并圖例的實現代碼_python
- 2021-12-07 關于postman上傳文件執行成功而使用collection?runner執行失敗的問題_相關技巧
- 2022-12-10 C++的std::vector<bool>轉儲文件問題_C 語言
- 2022-10-19 python列表[list]和元組(tuple)詳情_python
- 2022-12-22 React?Hooks?-?useContetx和useReducer的使用實例詳解_React
- 2023-11-23 寶塔數據庫過大導入失效解決方案
- 2022-07-06 R語言繪制條形圖及分布密度圖代碼總結_python
- 2022-06-17 Go語言使用Request,Response處理web頁面請求_Golang
- 最近更新
-
- 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同步修改后的遠程分支