Python Len() - Programiz

Tutorials Courses Python JavaScript TypeScript SQL HTML CSS C C++ Java R Ruby RUST Golang Kotlin Swift C# DSA

Become a certified Python programmer.

ENROLL

Popular Tutorials

Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python

Popular Examples

Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year Explore Python Examples

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Programiz Pro Logo

Created with over a decade of experience.

  • Learn
  • Practice
  • Compete
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA Learn C Learn C++ Learn Java View all Courses on Programiz Pro Logo Python Basics Python Intermediate C++ Basics C++ Intermediate C++ OOP C Programming Java Basics Java Intermediate Java OOP View all Courses on Programiz Pro Logo Python Challenges JavaScript Challenges Java Challenges C++ Challenges C Challenges View all Challenges on Programiz Pro Logo 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 on Programiz Pro Logo Learn C Learn C++ Learn Java Python JavaScript TypeScript SQL HTML CSS C C++ Java More languages

Become 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 Tutorials

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Python JavaScript C C++ Java R Kotlin

Become 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 Examples

Built-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: 3

len() 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: 4

Visit 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: 3

Visit 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 blank

Your 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