Cheap VPS host selection
Provide server host evaluation information

Introduction to the usage of len () in python

In Python, len() Is a built-in function that returns the length of the passed in object or the number of elements. It is applicable to various types of objects, including strings, lists, tuples, dictionaries, etc. The following is about len() Some common uses of functions:

Get the length of the string:

 message = "Hello, World!" length = len (message) print (length) #Output: 13

Get the length of the list:

 numbers = [ one , two , three , four , five ] length = len (numbers) print (length) #Output: 5

Get the length of tuple:

 fruits = ( "apple" , "banana" , "orange" ) length = len (fruits) print (length) #Output: 3

Get the number of key value pairs of the dictionary:

 student_scores = { "Alice" : eighty-five , "Bob" : ninety-two , "Charlie" : seventy-eight } length = len (student_scores) print (length) #Output: 3

Get the number of elements of the collection:

 unique_numbers = { one , two , three , four , five } length = len (unique_numbers) print (length) #Output: 5

Get the number of elements of other iterable objects:

 range_obj = range ( one , six ) length = len (range_obj) print (length) #Output: 5

len() Function can easily obtain the length of an object or the number of elements. It is often used in scenarios such as loop traversal and determining whether it is empty. It should be noted that, len() The function is only applicable to objects with determinable length. It may not be applicable to some special types of objects, such as iterators or file objects.

Do not reprint without permission: Cheap VPS evaluation » Introduction to the usage of len () in python