Home > Article > Backend Development > 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.
string = "Hello, World!" length = len(string) print("字符串的长度为:", length)
The output result is:
字符串的长度为: 13
list = [1, 2, 3, 4, 5] length = len(list) print("列表的长度为:", length)
The output result is:
列表的长度为: 5
tuple = (1, 2, 3, 4, 5) length = len(tuple) print("元组的长度为:", length)
The output result is:
元组的长度为: 5
set = {1, 2, 3, 4, 5} length = len(set) print("集合的长度为:", length)
The output result is:
集合的长度为: 5
dict = {'name': 'Tom', 'age': 18, 'gender': 'male'} length_keys = len(dict.keys()) length_values = len(dict.values()) print("字典的键的个数为:", length_keys) print("字典的值的个数为:", length_values)
The output result is:
字典的键的个数为: 3 字典的值的个数为: 3
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!