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

python中len()的用法

作者:嗜神魔影   发布日期:2025-09-14   浏览:25

# 示例代码:使用 len() 函数获取字符串、列表、元组和字典的长度

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

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

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

# 获取字典的长度
dict_example = {'name': 'Alice', 'age': 25, 'city': 'Beijing'}
dict_length = len(dict_example)
print(f"字典 {dict_example} 的长度是: {dict_length}")

解释说明:

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

上一篇:python变量名

下一篇:python base64

大家都在看

python时间格式

python读取文件路径

staticmethod在python中有

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

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

Laravel 中文站