A tuple in Python is an ordered sequence of elements. While working with Python tuples, we often need the length of the tuple to iterate through its elements.
So, how to get the length of a tuple in Python?
To get the length of a tuple in Python we use the inbuilt len() function. You just have to pass the name of the tuple as len()
parameter and it will return you the length of the tuple.
Example:
numbers = (1, 2, 3, 4, 5) length = len(numbers) print('Length of the tuple is: ', length)
Output:
Length of the tuple is: 5
If the tuple is empty, the len()
function returns 0.
Example:
empty_tuple = () length = len(empty_tuple) print('Length of the tuple is: ', length)
Output:
Length of the tuple is: 0