In this tutorial we will learn about the python len() method and its uses with examples.
What is the python len() Method?
The Python len() method is a built-in Python method that returns the object’s length. It’s also known as the python length method.
The syntax of python len() is:
Python len() Parameters Method
The len() method takes only one parameter as an argument.
- object – the name of the object that can be a sequence or a collection.
len() method can be used with almost all the objects like string, integer, tuple, list, range, dictionary, set, list, etc.
Example 1: How to use the len() method in python?
We can also use the len() method and the range() method to check the length of the range.
Example 2: How to use len() with range() method?
Example 3: How to use the len() method with dictionaries and sets?
Rules of len() method
- If a non-sequence object is passed, it will raise a TypeError exception because the len() method cannot deal with non-sequence objects.