Python len()函数 返回字符串的长度。
null
Python len()语法:
莱恩(弦)
len()参数:
它以字符串作为参数。
len()返回值:
它返回一个整数,即字符串的长度。
Python len()示例
示例1:带有元组、列表和字符串的Len()函数
python
# Python program to demonstrate the use of # len() method # Length of below string is 5 string = "geeks" print ( len (string)) # with tuple tup = ( 1 , 2 , 3 ) print ( len (tup)) # with list l = [ 1 , 2 , 3 , 4 ] print ( len (l)) |
输出:
534
示例2:Python len()类型错误
Python3
print ( len ( True )) |
输出:
TypeError: object of type 'bool' has no len()
示例3:带字典和集合的Python len()
Python3
# Python program to demonstrate the use of # len() method dic = { 'a' : 1 , 'b' : 2 } print ( len (dic)) s = { 1 , 2 , 3 , 4 } print ( len (s)) |
输出:
24
示例4:带有自定义对象的Python len()
Python3
class Public: def __init__( self , number): self .number = number def __len__( self ): return self .number obj = Public( 12 ) print ( len (obj)) |
输出:
12
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END