Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

len函数python用法

作者:资本principal   发布日期:2025-04-09   浏览:112

# 示例代码:len 函数在 Python 中的用法

# 1. 获取字符串的长度
string = "Hello, World!"
length_of_string = len(string)
print(f"字符串 '{string}' 的长度是: {length_of_string}")

# 2. 获取列表的长度
list_example = [1, 2, 3, 4, 5]
length_of_list = len(list_example)
print(f"列表 {list_example} 的长度是: {length_of_list}")

# 3. 获取元组的长度
tuple_example = (1, 2, 3, 'a', 'b', 'c')
length_of_tuple = len(tuple_example)
print(f"元组 {tuple_example} 的长度是: {length_of_tuple}")

# 4. 获取字典的长度(返回键的数量)
dict_example = {'name': 'Alice', 'age': 25, 'city': 'Beijing'}
length_of_dict = len(dict_example)
print(f"字典 {dict_example} 的长度是: {length_of_dict}")

# 5. 获取集合的长度
set_example = {1, 2, 3, 4, 5}
length_of_set = len(set_example)
print(f"集合 {set_example} 的长度是: {length_of_set}")

解释说明:

  • len() 函数用于返回对象(如字符串、列表、元组、字典、集合等)的长度或元素个数。
  • 对于字符串,len() 返回字符的数量。
  • 对于列表和元组,len() 返回元素的数量。
  • 对于字典,len() 返回键的数量。
  • 对于集合,len() 返回元素的数量。

上一篇:python 数组操作

下一篇:python的特性

大家都在看

python时间格式

python ord和chr

python中的yield

python list.pop

python的for i in range

npm config set python

python代码简单

python读取文件夹

python中turtle

python 输出时间

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站