歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python len() 函數 獲取字符串長度

Python len() 函數 獲取字符串長度

日期:2017/3/1 9:33:34   编辑:Linux編程

Python函數:len()

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

2:語法:len(str)

3:參數:
str:要計算的字符串、列表、字典、元組等

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

5:實例
5.1、計算字符串的長度:

>>> s = "hello good boy doiido"
>>> len(s)
21

5.2、計算列表的元素個數:

>>> l = ['h','e','l','l','o']
>>> len(l)
5

5.3、計算字典的總長度(即鍵值對總數):

>>> d = {'num':123,'name':"doiido"}
>>> len(d)
2

5.4、計算元組元素個數:

>>> t = ('G','o','o','d')
>>> len(t)
4

--------------------------------------分割線 --------------------------------------

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved