How to get the length of a tuple in Python?

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

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts