len() 函数返回给定对象中的元素数量,适用于字符串、列表、元组、字典和集合等各种对象。示例:字符串的长度为 11,列表的长度为 5 等。

len 在 Python 中的意思
len() 函数是 Python 中一个常用的函数,它返回给定对象中的元素数量。以下是它的用法和用法示例:
用法:
<code class="python">len(object)</code>
其中,object 是要计算长度的对象,它可以是:
立即学习“Python免费学习笔记(深入)”;
示例:
字符串:
<code class="python">my_string = "Hello World" length = len(my_string) # length = 11</code>
列表:
<code class="python">my_list = [1, 2, 3, 4, 5] length = len(my_list) # length = 5</code>
元组:
<code class="python">my_tuple = (1, 2, 3, 4, 5) length = len(my_tuple) # length = 5</code>
字典:
<code class="python">my_dict = {"name": "John", "age": 30}
length = len(my_dict) # length = 2</code>集合:
<code class="python">my_set = {1, 2, 3, 4, 5}
length = len(my_set) # length = 5</code>注意:
以上就是python中len的意思的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号