python3 len函数

Python3中的len函数用于返回对象(字符、列表、元组等)长度或项目个数。len(“Hello”) 返回5。

Python的len()函数用于返回对象(字符串、列表、元组等)的长度,以下是关于如何使用len()函数的详细解释:

1、字符串长度

python3 len函数

s = "Hello, world!"
length = len(s)
print(length)  # 输出:13

2、列表长度

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

3、元组长度

python3 len函数

my_tuple = (1, 2, 3, 4, 5)
length = len(my_tuple)
print(length)  # 输出:5

4、字典键值对数量

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

5、集合元素数量

python3 len函数

my_set = {1, 2, 3, 4, 5}
length = len(my_set)
print(length)  # 输出:5

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/649929.html

(0)
未希新媒体运营
上一篇 2024-05-23 18:45
下一篇 2024-05-23 18:48

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入