What is the usage of len in Python?

In Python, len() is a built-in function that is used to return the length or number of elements in an object. It can take objects like strings, lists, tuples, dictionaries, etc. as parameters and return the count of elements in the respective objects. For example:

my_string = 'Hello, World!'
print(len(my_string))  # 输出:13

my_list = [1, 2, 3, 4, 5]
print(len(my_list))  # 输出:5

my_tuple = (1, 2, 3, 4, 5)
print(len(my_tuple))  # 输出:5

my_dict = {'a': 1, 'b': 2, 'c': 3}
print(len(my_dict))  # 输出:3
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds