Python len() Method

Python 内置函数Python Built-in Functions


Description

Python len() method returns the length or number of items of an object (string, list, tuple, etc.).

Syntax

Syntax of len() method:

len( s )

Parameters

  • s -- object.

Return Value

Returns the length of the object.

Example

The following example demonstrates the usage of len():

Example

#!/usr/bin/env python # coding=utf-8 str = "example" print( len(str) ) # string length l = [1,2,3,4,5] print( len(l) ) # number of list elements

After executing the above code, the output is:

6
5

Python 内置函数Python Built-in Functions

Other Extensions