python中len函数

Python len()函数 (Python len() function)

len() function is a library function in Python, it is used to get the length of an object (object may a string, list, tuple, etc). It accepts an object and returns its length (total number of characters in case of a string, the total number of elements in case of an iterable).

len()函数是Python中的库函数,用于获取对象的长度(对象可以是字符串,列表,元组等)。 它接受一个对象并返回其长度(如果是字符串,则为字符总数,如果是可迭代的则为元素总数)。

Syntax:

句法:

    len(object)

Parameter:object – an object like string, list etc whose length to be calculated.

参数: object –一个对象,例如字符串,列表等,其长度将被计算。

Return value: int – returns length of the object.

返回值: int –返回对象的长度。

Example:

例:

    Input:
    a = "Hello world!"
    print(len(a))
    
    Output:
    12

Python code to get the length of an object (string, list etc)

Python代码获取对象的长度(字符串,列表等)

# python code to demonstrate an example
# of len() function

a = "Hello world!"          # string value
b = [10, 20, 30, 40, 50]    # list
c = ["Hello", "World!", "Hi", "Friends"] # list of strings
d = ("Hello", "World!", "Hi", "Friends") # Tuple

print("length of a: ", len(a))
print("length of b: ", len(b))
print("length of d: ", len(c))
print("length of c: ", len(d))

Output

输出量

length of a:  12
length of b:  5
length of d:  4
length of c:  4


翻译自: https://www.includehelp.com/python/len-function-with-example.aspx

python中len函数

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐