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.
1 2 |
myTuple = ("Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"); print(len(myTuple)); |
Output
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.
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.
1 2 3 4 5 |
myTuple = ("Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"); i=0; for t in myTuple: i=i+1; print(i); |
Output
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
- How to Find First and Last Element of Tuple in Python
- Unpack Tuple Pairs in List Using Python
- How To Find Index Of Element In Tuple Using Python
I hope you like this post on how to get the size of the tuple variable using Python.