Python len() Function: How to find length of the string
โก Smart Summary
Python len() is a built-in function that returns the number of items in an object, such as the characters in a string or the elements in a list, tuple, dictionary, or set.

What is the Python len() Function?
The len() function is a built-in function in Python. You can use len() to get the length of a given string, array, list, tuple, dictionary, and other objects. Because the number of elements is stored on the object rather than recalculated each time, len() helps you check size quickly and optimize the performance of your program.
Syntax of the Python len() Function
The Python len() function uses the following syntax:
len(value)
Parameters
value: The given object โ such as a string, list, tuple, dictionary, or set โ whose length you want to measure.
Return Value
len() returns an integer, that is, the number of characters in a string or the number of elements in an array, list, or collection. The exact meaning of the return value depends on the type of object passed to it:
- Strings: len() returns the number of characters in the string, including punctuation, spaces, and every kind of special character.
- Empty: An empty string or empty collection returns 0, because it contains zero elements.
- Collections: The len() built-in returns the number of elements stored in a collection.
- TypeError: len() depends on the type of the object passed to it. Passing a value that has no length, such as an integer, raises a TypeError.
- Dictionary: For a dictionary, each key-value pair is counted as one unit, so len() returns the number of keys.
Examples of the Python len() Function
The following examples show how to use the len() function to find the length of different Python objects, including a string, a list, a tuple, a dictionary, and an array.
Example 1: How to Find the Length of a String
# testing len()
str1 = "Welcome to Guru99 Python Tutorials"
print("The length of the string is :", len(str1))
Output:
The length of the string is : 35
Example 2: How to Find the Length of a List
# to find the length of the list
list1 = ["Tim","Charlie","Tiffany","Robert"]
print("The length of the list is", len(list1))
Output:
The length of the list is 4
Example 3: How to Find the Length of a Tuple
# to find the length of the tuple
Tup = ('Jan','feb','march')
print("The length of the tuple is", len(Tup))
Output:
The length of the tuple is 3
Example 4: How to Find the Length of a Dictionary
# to find the length of the Dictionary
Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25}
print("The length of the Dictionary is", len(Dict))
Output:
The length of the Dictionary is 4
Example 5: How to Find the Length of an Array
# to find the length of the array
arr1 = ['Tim','Charlie','Tiffany','Robert']
print("The length of the Array is", len(arr1))
Output:
The length of the Array is 4
How to Find the Length of a String Without len() in Python
The len() function is the fastest and most readable way to measure a string, but you can also count characters manually. This is a common coding-interview exercise, and every method simply loops through the string while increasing a counter.
The most common approach uses a for loop:
# length of a string without len()
mystr = "Guru99"
count = 0
for char in mystr:
count += 1
print("The length is", count)
Output:
The length is 6
A shorter option uses the sum() function with a generator expression, adding 1 for every character in the string:
mystr = "Guru99"
length = sum(1 for char in mystr)
print("The length is", length)
Output:
The length is 6
Other ways to count characters without len() include the following:
- A while loop that slices the string until it becomes empty, incrementing a counter on each pass.
- The enumerate() function, which tracks the index of every character as it iterates.
- A recursive function that returns 1 plus the length of the remaining substring.
These manual methods are slower than the built-in len(), so reserve them for learning or interview practice. In everyday code, len() remains the fastest and clearest option.
How Does the Python len() Function Work Internally?
Understanding how len() works explains why it is so fast. When you call len() on an object, Python does not scan every element one by one. Instead, it calls the object special __len__() method, which means the expression len(x) is equivalent to x.__len__().
Built-in containers such as strings, lists, tuples, dictionaries, and sets store their own size as an attribute. Each time you add or remove an element, Python updates this stored count. When len() is called, it simply reads that pre-stored value and returns it.
Because the length is already known, len() runs in constant time, written as O(1). It takes the same amount of time whether the object holds five items or five million. You can also add len() support to your own classes by defining a __len__() method that returns an integer:
class Team:
def __len__(self):
return 11
squad = Team()
print(len(squad))
Output:
11
This design is why len() behaves consistently across every built-in type, as well as any custom object that implements a __len__() method.
ยป Learn more about Python String methods
