What data types is the LEN function suitable for?

王林
Release: 2024-01-28 08:18:06
Original
878 people have browsed it

What data types is the LEN function suitable for?

The LEN function is a commonly used function that can be used to obtain the length of strings, lists, tuples and other types of data. The following will introduce in detail the data types that the LEN function can handle and provide corresponding code examples.

  1. String (str):
    A string is a sequence composed of several characters. You can use the LEN function to get the length of the string. The sample code is as follows:
string = "Hello, World!"
length = len(string)
print("字符串的长度为:", length)
Copy after login

The output result is:

字符串的长度为: 13
Copy after login
  1. List:
    The list is a variable sequence type that can contain any type The data. The LEN function is used to get the number of elements in a list. The sample code is as follows:
list = [1, 2, 3, 4, 5]
length = len(list)
print("列表的长度为:", length)
Copy after login

The output result is:

列表的长度为: 5
Copy after login
  1. Tuple:
    Tuple is an immutable sequence type, different from list Similarly, any type of data can be included. The LEN function can be used to get the number of elements in a tuple. The sample code is as follows:
tuple = (1, 2, 3, 4, 5)
length = len(tuple)
print("元组的长度为:", length)
Copy after login

The output result is:

元组的长度为: 5
Copy after login
  1. Set:
    A set is an unordered, non-repeating data type. Index operations are not supported. The LEN function can be used to get the number of elements in a collection. The sample code is as follows:
set = {1, 2, 3, 4, 5}
length = len(set)
print("集合的长度为:", length)
Copy after login

The output result is:

集合的长度为: 5
Copy after login
  1. Dictionary (dict):
    The dictionary is a data structure of key-value pairs, and the key must be only. The LEN function cannot directly obtain the length of the dictionary, but it can obtain the length indirectly by obtaining the number of keys or values ​​of the dictionary. The sample code is as follows:
dict = {'name': 'Tom', 'age': 18, 'gender': 'male'}
length_keys = len(dict.keys())
length_values = len(dict.values())
print("字典的键的个数为:", length_keys)
print("字典的值的个数为:", length_values)
Copy after login

The output result is:

字典的键的个数为: 3
字典的值的个数为: 3
Copy after login

In summary, the LEN function can handle data types such as strings, lists, tuples, sets, and dictionaries, and can Help us get their length conveniently. In practical applications, the LEN function is very useful when processing data.

The above is the detailed content of What data types is the LEN function suitable for?. 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