📏

Overview of Python's len() Function

Apr 24, 2025

Python len() Function Overview

Definition

  • The len() function returns the number of items in an object.
  • For strings, it returns the number of characters.

Syntax

len(object)
  • object: Required parameter. Must be a sequence (like a string, bytes, tuple, list, or range) or a collection (like a dictionary, set, or frozen set).

Examples

List Example

mylist = ["apple", "banana", "cherry"] x = len(mylist) # Returns 3

String Example

mystring = "Hello" x = len(mystring) # Returns 5

Additional Resources

Study and Practice

  • Practice using the len() function with different Python sequences and collections to understand its general use.
  • Explore more Python tutorials and examples on W3Schools.

Certification and Learning Paths


This guide provides an overview of how to use the len() function in Python, offering examples and syntax details to support learning and application.