len函數用于獲取一個對象的長度或元素個數。它的使用方法如下:
string = "Hello World"
length = len(string)
print(length) # 輸出:11
list1 = [1, 2, 3, 4, 5]
length = len(list1)
print(length) # 輸出:5
tuple1 = (1, 2, 3, 4, 5)
length = len(tuple1)
print(length) # 輸出:5
set1 = {1, 2, 3, 4, 5}
length = len(set1)
print(length) # 輸出:5
dict1 = {"name": "John", "age": 25, "city": "New York"}
length = len(dict1)
print(length) # 輸出:3
number = 12345
length = len(number)
print(length) # TypeError: object of type 'int' has no len()
總之,len函數可以用于獲取字符串、列表、元組、集合、字典等對象的長度或元素個數。