Python Len() - Programiz
Maybe your like
Become a certified Python programmer.
ENROLLPopular Tutorials
Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning PythonPopular Examples
Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year Explore Python ExamplesReference Materials
Built-in Functions List Methods Dictionary Methods String Methods View allCreated with over a decade of experience.
- Learn
- Practice
- Compete
Certification Courses
Created with over a decade of experience and thousands of feedback.
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA View all Courses onBecome a certified Python programmer.
Try Programiz PRO!Popular Tutorials
Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python All Python TutorialsReference Materials
Built-in Functions List Methods Dictionary Methods String Methods View all Python JavaScript C C++ Java R KotlinBecome a certified Python programmer.
Try Programiz PRO!Popular Examples
Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year All Python ExamplesBuilt-in Functions
- Python abs()
- Python any()
- Python all()
- Python ascii()
- Python bin()
- Python bool()
- Python bytearray()
- Python callable()
- Python bytes()
- Python chr()
- Python compile()
- Python classmethod()
- Python complex()
- Python delattr()
- Python dict()
- Python dir()
- Python divmod()
- Python enumerate()
- Python staticmethod()
- Python filter()
- Python eval()
- Python float()
- Python format()
- Python frozenset()
- Python getattr()
- Python globals()
- Python exec()
- Python hasattr()
- Python help()
- Python hex()
- Python hash()
- Python input()
- Python id()
- Python isinstance()
- Python int()
- Python issubclass()
- Python iter()
- Python list() Function
- Python locals()
- Python len()
- Python max()
- Python min()
- Python map()
- Python next()
- Python memoryview()
- Python object()
- Python oct()
- Python ord()
- Python open()
- Python pow()
- Python print()
- Python property()
- Python range()
- Python repr()
- Python reversed()
- Python round()
- Python set()
- Python setattr()
- Python slice()
- Python sorted()
- Python str()
- Python sum()
- Python tuple() Function
- Python type()
- Python vars()
- Python zip()
- Python __import__()
- Python super()
Python Tutorials
- Python del Statement
- Python Lists Vs Tuples
- Python Data Types
- Python Operator Overloading
- Python range() Function
- Python tuple()
Python len() The len() function returns the length (the number of items) of an object.
Example
languages = ['Python', 'Java', 'JavaScript'] length = len(languages) print(length) # Output: 3len() Syntax
The syntax of len() is:
len(s)len() Argument
The len() function takes a single object as argument. It can be:
- Sequence - list, tuple, string, range, etc.
- Collection - set, dictionary etc.
len() Return Value
It returns an integer (the length of the object).
Example 1: Working of len() with Tuples, Lists and Range
x = [1, 2, 3] print(len(x)) # Output: 3 y = (1, 2, 3) print(len(y)) # Output: 3 z = range(8, 20, 3) print(len(z)) # Output: 4Visit these pages to learn more about:
- Python range()
- Python Lists
- Python Tuples
Example 2: len() with Strings, Dictionaries and Sets
text = 'Python' print(len(text)) # Output: 6 person = {"name": 'Amanda', "age": 21} print(len(person)) # Output: 2 animals = {'tiger', 'lion', 'tiger', 'cat'} print(len(animals)) # Output: 3Visit these pages to learn more about:
- Python Strings
- Python Dictionaries
- Python Sets
len() with User-defined Objects
The len() function internally calls the object's __len__() method. You can think of len() as:
def len(s): return s.__len__()Therefore, we can make len() work for a user-defined object by implementing the ___len___() method.
Example 3: len() with User-defined Object
class Session: def __init__(self, number = 0): self.number = number def __len__(self): return self.number # default length is 0 session1 = Session() print(len(session1)) # Output: 0 session2 = Session(6) print(len(session2)) # Output: 6 Previous Tutorial: Python locals() Next Tutorial: Python max() Share on: Did you find this article helpful?Sorry about that.
How can we improve it? Feedback * Leave this field blankYour builder path starts here. Builders don't just know how to code, they create solutions that matter.
Escape tutorial hell and ship real projects.
Try Programiz PRO- Real-World Projects
- On-Demand Learning
- AI Mentor
- Builder Community
Python References
Python Library
Python range() Function
Python Library
Python print()
Python Library
Python object()
Python Library
Python map() Function
Tag » What Does Len Do In Python
-
Using The Len() Function In Python
-
Python Len() Function - W3Schools
-
A Step-By-Step Guide To Len Python | Career Karma
-
Python String Length | Len() - GeeksforGeeks
-
Python Len() Function: How To Find Length Of The String - Guru99
-
Python Len() Method (With Examples) - TutorialsTeacher
-
What Is Len() In Python? How To Use The Len() Function To Find The ...
-
Introduction To Python's Len() Function - YouTube
-
What Does The Len() Function Do In Python? - Quora
-
What Is Python Len() Function Used For? - Toppr
-
Python List Len() Method - Tutorialspoint
-
What Does Len() Mean In Python, And What Is The Difference Between ...
-
Len() Function In Python - Scaler Topics
-
Python String Length | Len() Method With Example - STechies