Syntax: len(string) Return: It returns an integer which is the length of the string.
Syntax: len(string)
Return: It returns an integer which is the length of the string.
Python 中带有字符串的 len() 方法。
string = "Geeksforgeeks" print(len(string))
输出:
13
这里我们计算元组和列表的长度。
# Python program to demonstrate the use of # len() method # with tuple tup = (1,2,3) print(len(tup)) # with list l = [1,2,3,4] print(len(l))
3 4
print``(``len``(``True``))
TypeError: object of type 'bool' has no len()
# Python program to demonstrate the use of # len() method dic = {'a':1, 'b': 2} print(len(dic)) s = { 1, 2, 3, 4} print(len(s))
2 4
class Public: def __init__(self, number): self.number = number def __len__(self): return self.number obj = Public(12) print(len(obj))
12
时间复杂度:
len()函数的时间复杂度为 O(1)。在平均摊销情况下
原文链接:codingdict.net