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

python中len的用法例子

作者:鱼宝宝睡觉觉   发布日期:2026-04-30   浏览:58

# 示例代码:Python 中 len() 的用法

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

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

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

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

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

上一篇:在线python运行

下一篇:python rstrip

大家都在看

python 二维码识别

python excel 库

python时间格式

pythoneval函数用法

列表切片操作python

python读取文件路径

staticmethod在python中有

python 保存json文件

python的assert基本用法

python开发windows应用程序

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

Laravel 中文站