len()是python中的内置函数。 您可以使用len()获得给定字符串,数组,列表,元组,字典等的长度。
语法:
len(value)
参数:
value:您想要的长度的给定值。
返回值
将返回一个整数值,即给定的字符串,数组,列表或集合的长度。
示例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