zoukankan      html  css  js  c++  java
  • Python len() 方法

    描述

    Python len() 方法返回对象(字符串、列表、元组、字典等)长度或项目个数。

    语法

    len() 方法语法:

    len(obj)

    参数

    • obj -- 对象(字符串、列表、元组、字典等)。

    返回值

    返回对象长度。

    实例

    以下几个实例展示了 len() 方法的使用方法:

    1.字符串长度:

    >>>S = "runoob"
    >>> len(S)             # 字符串长度
    6
    >>> l = [1,2,3,4,5]
    >>> len(l)               # 列表元素个数
    5

    2.列表长度:

    #!/usr/bin/python3
    
    list1 = ['Google', 'Runoob', 'Taobao']
    print (len(list1))
    list2=list(range(5)) # 创建一个 0-4 的列表
    print (len(list2))
    

    以上实例输出结果如下:

    3
    5
    

    3.元祖长度:

    #!/usr/bin/python3
     
    tuple1, tuple2 = (123, 'xyz', 'zara'), (456, 'abc')
     
    print("First tuple length : ", len(tuple1))
    print( "Second tuple length : ", len(tuple2))
    

    以上实例输出结果如下:

    First tuple length :  3
    Second tuple length :  2
    

    4.字典长度:

    #!/usr/bin/python3
     
    dict = {'Name': 'Zara', 'Age': 7};
    print ("Length : %d" % len (dict))
    

    以上实例输出结果如下:

    Length : 2
  • 相关阅读:
    token
    跨域问题???
    简单使用express
    深拷贝 浅拷贝
    node表单提交初知识!
    11.29
    11.28
    11.27
    11.26每日总结
    11.25每日总结
  • 原文地址:https://www.cnblogs.com/wushuaishuai/p/7686909.html
Copyright © 2011-2022 走看看