search

How to find the length of a tuple in Python?

To find the length of a tuple in Python, you can use the built-in function len(). Here's an example:

my_tuple = (1, 2, 3, 4, 5)
length = len(my_tuple)
print(length)
# Output: 5

In this example, the len() function is used to return the number of elements in the tuple my_tuple. The resulting value is then stored in the variable length and printed.

The len() function takes a sequence, such as a tuple, as an argument and returns an integer representing the length of the sequence, which is the count of elements in the tuple.

Note that the length of a tuple represents the number of elements in the tuple, regardless of the data type or value of the elements. So, in the example above, the length of the tuple (1, 2, 3, 4, 5) is 5, as it contains five elements.

Related Questions You Might Be Interested