亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 字符串长度 | len() 方法示例


len() 是python的内置函数。可以使用len()获取给定字符串、数组、列表、元组、字典等的长度。

您可以使用 len 函数来优化程序的性能。永远不会计算对象中存储的元素数量,因此 len 有助于提供元素数量。

语法:

len(value)

参数:

价值 :你想要的给定值的长度。

返回值

它将返回一个整数值,即给定字符串、数组、列表或集合的长度。

各种类型的返回值:

字符串:

它返回字符串中的字符数,包括标点符号、空格和所有类型的特殊字符。但是,在使用 Null 变量的 len 时应该非常小心。

空:

Empty 是第二个返回调用,它有零个字符,但它始终是 None。

收藏:

len 内置函数返回集合中元素的数量。

类型错误:

Len 函数取决于传递给它的变量的类型。 Non-Type 没有任何内置支持。

字典:

对于字典,每一对都算作一个单元。但是,值和键不是独立的。

示例1:如何求给定字符串的长度?

# testing len() 
str1 = "Welcome to  Guru99 Python Tutorials"
print("The length of the string  is :", len(str1))

输出:

The length of the string  is : 35

示例2:如何在python中查找列表的长度?

# to find the length of the list

list1 = ["Tim","Charlie","Tiffany","Robert"]

print("The length of the list is", len(list1))

输出:

The length of the list is 4

示例3:如何在python中求元组的长度

# to find the length of the tuple

Tup = ('Jan','feb','march')

print("The length of the tuple is", len(Tup))

输出:

The length of the tuple is 3

示例4:如何在Python中查找字典的长度?

# to find the length of the Dictionary

Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25}

print("The length of the Dictionary is", len(Dict))

输出:

The length of the Dictionary is 4

例子5:如何在python中求数组的长度

# to find the length of the array

arr1 = ['Tim','Charlie','Tiffany','Robert']

print("The length of the Array is", len(arr1))

输出:

The length of the Array is 4

总结:


Python

  1. Java 中的 String Length() 方法:如何通过示例查找
  2. Java String charAt() 方法及示例
  3. Java String contains() 方法 |用示例检查子字符串
  4. Java String endsWith() 方法及示例
  5. Python String strip() 函数与示例
  6. 带有示例的 Python 字符串计数()
  7. Python String format() 举例说明
  8. Python 字符串长度 | len() 方法示例
  9. Python String find() 方法及示例
  10. Python 教程中的收益:生成器和收益与返回示例
  11. 集合中的 Python 计数器示例
  12. Python 中的 Enumerate() 函数:循环、元组、字符串(示例)