Ernest Jelinskis
PRO
last year
Ernestcountry asked

What does the len() function do in Python?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hello Ernest, really nice question.

In Python, len() is a built-in function that tells you how many items are in something.

You give it an object like a list, string, tuple, etc., and it returns an integer:

languages = ['Python', 'JavaScript', 'C++']
print(len(languages))   # 3

Here, len(languages) is 3 because there are three items in the list.

It works the same way with strings:

word = "Hello"
print(len(word))   # 5

And with an empty list:

items = []
print(len(items))  # 0

So the simple way to remember it:

len(x) gives you “how many things are inside x”.

If you have further questions, I'm here to help.

Python
This question was asked as part of the Getting started with Python course.