Python Tuple – len() Method
While working with tuples many times we need to find the length of the tuple, and, instead of counting the number of elements with loops, we can also use Python len(). We will learn about the len() method used for tuples in Python.
Python tuple len() Method
Syntax: len(object)
Parameters:
- object: Any iterable like Tuple, List, etc.
Return type: the number of elements in the tuple.
Example
Tuple =( 1, 0, 4, 2, 5, 6, 7, 5)
Input: len(Tuple)
Output: 8
Explanation:
The len() method returns the total number of elements in the given tuple.
Example 1
Here we are finding the length of a particular tuple.
Python3
# Creating tuples Tuple = ( 1 , 3 , 4 , 2 , 5 , 6 ) res = len ( Tuple ) print ( 'Length of Tuple is' , res) |
Output:
Length of Tuple is 6
Example 2
Here we are finding the number of elements of the tuple that constitutes string elements.
Python3
# Creating tuples Tuple = ( "Geeks" , "For" , "Geeks" , "GeeksForGeeks" ) res = len ( Tuple ) print ( 'Length of Tuple is' , res) |
Output:
Length of Tuple is 4
Example 3
Here we are finding the length of the nested tuples.
Python3
# alphabets tuple alphabets = (( 'G' , 'F' , 'G' ), ( 'g' , 'f' , 'g' ), ( 'g' , 'F' , 'g' ), 'GfG' , 'Gfg' ) res = len (alphabets) print ( 'Length of Tuple is' , res) res_nested = len (alphabets[ 0 ]) print ( 'Length of nested tuple is' , res_nested) |
Output:
Length of Tuple is 5 Length of nested tuple is 3
Please Login to comment...