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
  • 相关阅读:
    JavaScript中的数组
    JavaScript中的对象
    Highcharts中设置x轴为时间的写法
    CSS 选择器(基础)
    DOM节点
    平衡木蜻蜓
    python2.7 qt4
    C语言优先级
    树莓派与stm32通信
    linux下USB转串口配置
  • 原文地址:https://www.cnblogs.com/wushuaishuai/p/7686909.html
Copyright © 2011-2022 走看看