# len在Python中用于获取对象的长度或元素个数
# 示例1: 获取字符串的长度
string = "Hello, World!"
length_of_string = len(string)
print(f"字符串的长度是: {length_of_string}") # 输出: 字符串的长度是: 13
# 示例2: 获取列表的长度
list_example = [1, 2, 3, 4, 5]
length_of_list = len(list_example)
print(f"列表的长度是: {length_of_list}") # 输出: 列表的长度是: 5
# 示例3: 获取字典的长度(即键的数量)
dict_example = {'a': 1, 'b': 2, 'c': 3}
length_of_dict = len(dict_example)
print(f"字典的长度是: {length_of_dict}") # 输出: 字典的长度是: 3
上一篇:python dict删除key
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站