Python len() 函数:如何查找字符串的长度
Python len() 函数
len() 是 Python 中的内置函数。您可以使用 len() 获取给定字符串、数组、列表、元组、字典等的长度。您可以使用 len 函数来优化程序的性能。对象中存储的元素数量从不计算,因此 len 有助于提供元素数量。
句法
len(value)
参数
价值观:您想要的长度的给定值。
返回值
它将返回一个整数值,即给定字符串、数组、列表或集合的长度。
各种类型的返回值:
字串:
它返回字符串中的字符数,包括标点符号、空格和所有类型的特殊字符。但是,使用 Null 变量的 len 时应非常小心。
空:
Empty 是第二个返回调用,它有零个字符,但它始终为 None。
类别:
len 内置函数返回集合中的元素数量。
TypeError:
Len 函数取决于传递给它的变量的类型。非类型没有任何内置支持。
字典:
对于字典来说,每一对都算作一个单位,但是值和键并不是独立的。
例 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
Example 2: How to find the length of the list in 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
Example 3: How to find the length of a tuple in 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
Example 5: How to find the length of the array in 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
总结
- len() 是内置函数 Python 中的函数。您可以使用 len() 获取给定字符串、数组、列表、元组、字典等的长度。
- 值:您想要的长度的给定值。
- 返回值a返回一个整数值,即给定字符串、数组、列表或集合的长度。