Python中len函数的用法是甚么
len函数是Python内置函数之一,主要用于返回对象(字符串、列表、元组等)的长度还是项数。
基本用法以下: len(object)
其中:
示例:
# 字符串
s = "Hello, World!"
print(len(s)) # 输出:13
# 列表
lst = [1, 2, 3, 4, 5]
print(len(lst)) # 输出:5
# 元组
tpl = (1, 2, 3, 4, 5)
print(len(tpl)) # 输出:5
另外,对一些特殊对象,可以通太重写对象的__len__()
方法来自定义len函数的行动。
TOP