This article mainly briefly introduces the use of the len() function in Python, including small examples of use in four situations. It is the basic knowledge in Python learning. Friends in need can refer to it
Function: len()
1: Function: Return the length of string, list, dictionary, tuple, etc.
2: Syntax: len(str)
3: Parameters:
str: String, list, dictionary, tuple, etc. to be calculated
4: Return Value: The length of string, list, dictionary, tuple and other elements
5: Example
5.1. Calculate the length of string:
>>> s = "hello good boy doiido" >>> len(s) 21
5.2. Calculate the number of elements in the list:
>>> l = ['h','e','l','l','o'] >>> len(l) 5
5.3. Calculate the total length of the dictionary (that is, the total number of key-value pairs):
>>> d = {'num':123,'name':"doiido"} >>> len(d) 2
5.4. Calculate the number of tuple elements:
>>> t = ('G','o','o','d') >>> len(t) 4
【Related recommendations】
1. In-depth understanding of the special function __len__(self) in python
2. Required Little knowledge to master - Detailed explanation of Python len examples
3. Example tutorial on using python special class methods
4. Python magic methods __getitem__, __setitem__, __delitem__, and __len__ are introduced respectively
The above is the detailed content of Summarize the usage examples of len() function in Python. For more information, please follow other related articles on the PHP Chinese website!