Summarize the usage examples of len() function in Python

零下一度
Release: 2017-05-23 14:46:19
Original
4842 people have browsed it

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
Copy after login

5.2. Calculate the number of elements in the list:

>>> l = ['h','e','l','l','o']
>>> len(l)
5
Copy after login

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
Copy after login

5.4. Calculate the number of tuple elements:

>>> t = ('G','o','o','d')
>>> len(t)
4
Copy after login

【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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template