溫馨提示×

Python中l(wèi)en函數怎么使用

小億
87
2024-02-21 14:09:14
欄目: 編程語言

len()函數用于返回對象的長度或元素個數。其語法為:

len(object)

其中,object表示要獲取長度的對象,可以是字符串、列表、元組、字典等。

例如,獲取字符串的長度:

string = "Hello, World!"
length = len(string)
print(length)  # 輸出:13

獲取列表的長度:

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

獲取字典的鍵值對個數:

my_dict = {"a": 1, "b": 2, "c": 3}
length = len(my_dict)
print(length)  # 輸出:3

0