Find Length of Tuple or Get Items Size Using Python

Last Updated on June 17, 2021 by Roshan Parihar

In this tutorial, learn how to find length of tuple in Python. The short answer is: use Python len() to get the length of a tuple variable. The length of the tuple can also be found using the for loop whose method is given here.

The len() function is the easiest way to know the size of any variable using Python. You may also like to read how to find the size of the list variable using Python.

Check the examples given with the two ways to find the length. So, let’s start learning each section one by one with the examples given here.

How to Find Length of Tuple Using Python len()

If you want to find the length of the tuple variable, you have to use the Python len() function. The function takes a single argument which is the tuple variable. However, you can find the length of any Python variable by using this function.

See the example below with the single tuple variable. The len() function contains the tuple variable as the argument.

Output

6

There are 6 elements in the tuple variable. The above example prints the size of the tuple as 6. No matter whether the variable contains the string or integer. The len() count both the string and the integer element of the tuple.

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

Get Size of a Tuple Using For Loop in Python

In addition to the above method, you can also get the length of the tuple variable using the loop. To find the length of the tuple variable, you have to use the for loop with the method as given in the example below.

To perform this task using the for loop, you have to initiate a variable with zero(0). After that, increment the variable with 1 as given below.

Output

6

The above example prints the size of the tuple variable using the for loop of Python. The print function contains in the above example is outside of the for loop.

DOWNLOAD Free PYTHON CHEAT SHEET

You May Also Like to Read

I hope you like this post on how to get the size of the tuple variable using Python.

Reference
W3resource to Calculate Tuple Size in Python