溫馨提示×

Python中len函數有什么作用

小億
165
2023-10-11 00:28:13
欄目: 編程語言

len函數用于計算對象的長度或元素的個數。它適用于多種數據類型,包括字符串、列表、元組、字典等。

在字符串中,len函數返回字符串中字符的個數。

string = "Hello World"
print(len(string))  # 輸出:11

在列表、元組中,len函數返回列表或元組中元素的個數。

list = [1, 2, 3, 4, 5]
print(len(list))  # 輸出:5
tuple = (1, 2, 3, 4, 5)
print(len(tuple))  # 輸出:5

在字典中,len函數返回字典中鍵值對的個數。

dictionary = {"name": "John", "age": 25, "gender": "male"}
print(len(dictionary))  # 輸出:3

在其他類型的對象中,len函數可能返回對象的特定屬性的個數,具體取決于對象的實現方式。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女