How to Use len() Function in Python
The len function in python that counts the number of items in an object. In this article, we will discuss len function in python and how to use it with list, tuple, array, string, range, and with sets.
In Python, the len() function is essential and easy to use. It helps you find out how many items are in a data structure like a list, string, tuple, or dictionary. Simply put, len(your_object) tells you how many elements are in your_object, making it a fundamental tool for managing and navigating through data in Python.
Table of Content
Best-suited Python courses for you
Learn Python with these high-rated online courses
What is the len() function in Python?
Definition
The len() function is one of the built-in functions in python that counts the number of items in an object (string, list, tuple, set, or sequence). It returns the number of objects in an integer format.
- String: It will return the number of characteristics (including spaces).
- List or Tuple: It will return the number of items (that are separated by commas, not the characteristics).
Syntax
The syntax for the length function in python is straightforward; you just have to give the variable name inside the len() function, i.e.,
len(object)
Parameter
Object β Required β Must be a sequence or collection
Sequence: string, byte, tuple, list, etc.
Collection: dictionary, set
Until now, we have a clear understanding of the len function in python; now, we will take some examples to understand better how it is used.
Examples
Example-1: Len() function with List
#find the length of the list#define list
color = ['blue', 'green', 'red', 'orange', 'yellow']
len(color)
#find the length of the tuple#define tuple
bike = ('yamaha', 'honda', 'tvs', 'RE')
len(bike)
Output
Must Read: Python Tuple
Must Read: Python List vs. Python Tuple
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Example β 3: Len() function with String
#find the length of the string#define string
company = ('we are shiksha online')
len(company)
Output
Must Read: Python String
Must Read: String Template Class in Python
Convert Python List to String | Modify Python String |
Python String Practice Problems | Reverse a String in Python |
Example β 4: Len() function with Range
#find the length of the range#use range function
r = range(1, 100)
len(r)
#find the length of the dictionary#define a dictionary
roll_no = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
len(roll_no)
#find the length of the set#define a set
car = {'tata', 'hyundai', 'skoda'}
len(car)
Output
Must Read: Python Sets
Conclusion
The len() function in python is used to count the number of items in that object. In this article, we have learned how to use python len with the help of several examples.
Hope you will like the article.
Top Trending Article
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |
FAQs
What is the len() function in Python and how is it used?
The len() function in Python is a built-in function that returns the length of an object, such as the number of items in a list, characters in a string, or elements in a tuple. It's used by simply calling len(object), where an object can be a sequence (like a string, list, tuple) or a collection (like a dictionary, set, or frozen set).
Can len() be used with user-defined classes?
Yes, len() can be used with user-defined classes. You need to implement the .__len__() method in your class definition. When len() is called with an instance of your class as its argument, Python internally calls the .__len__() method of that instance to determine its length.
Are there any data types with which len() cannot be used?
Yes, len() cannot be used with all data types. For instance, it raises a TypeError when used with data types that don't inherently support a concept of length, such as integers, floats, and Boolean types. len() also cannot be used directly with iterators and generators, as their lengths aren't predetermined and can be infinite.
How does len() work with third-party data types like NumPy arrays and pandas DataFrames?
When used with a NumPy array, len() returns the size of the first dimension. For a pandas DataFrame, it returns the number of rows. The behavior of len() with these data types depends on how their respective classes have implemented the .__len__() method.
What happens if len() is used on an empty object?
If len() is used on an empty object like an empty list, string, or tuple, it returns 0. This is because there are no elements in the object to count.
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio