Tuple is a data structure that allows you to store multiple values in a single variable. All the elements inside it are ordered and unchangeable like a list. Let’s say you have a tuple and want to find its length then you can easily find using the len() function. In this tutorial, you will know how to find the length of a tuple in python through steps.
Steps to find the length of a tuple in python
Let’s know all the steps that you will follow to find the length of a tuple in python.
Step 1: Create a sample tuple
The first step is to create a dummy tuple that will be used to find its length in the next step. You can create a tuple using the square brackets. Use the below line of code to create it.
sample_tuple = (10, 20, 30, "Data", "Science","Learner") print(sample_tuple)
Step 2: Find the length of a tuple in python using len() function
After the creation of the tuple let’s find the length of the tuple using the len() function. But before that let us know the syntax of the len() function.
Here the argument is the object that will be a collection like a list, tuple, etc. It will return the total number of elements present in the collection.
To find the length of the tuple you have to just pass your tuple inside the len() object.
Execute the below lines of code to find the len() of the tuple.
Step 3: Print the length of the tuple
After implementing the second step let’s print out the length of the tuple. To do so you will use the print() function.
sample_tuple = (10, 20, 30, "Data", "Science","Learner") print(len(sample_tuple))
You can easily find the length of a tuple using the len() function. This function also allows you to check whether the tuple is empty or not if the return value is 0. These are steps to find the length of a tuple.
I hope you must have understood how to use it. If you have any queries then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.