BBampaa

Lessons › Python

Python lesson

len()

There are five orders, so len(orders) gives 5. It works on text too: len("Lagos") is 5, one for each letter.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

len() counts the items in a list. Put the list inside the brackets:

orders = [70, 15, 24, 35, 140]
print(len(orders))
print(len("Lagos"))

Output:

5
5

Key points

len(orders)How many items are in the list.
len("Lagos")How many characters are in the text: 5.
len([])An empty list has 0 items.

Put together: print(len(orders)) shows how many orders there are.

Practice

Try each one in your head first, then open the answer.

1. Build a line that shows how many items are in prices.
print(len(prices))
2. Build a line that stores the number of customers in a variable called n.
n = len(customers)
3. Print how many sales there are.
sales = [96, 120, 175, 120, 100]
print(len(sales))
4. Print how many letters are in the word Earbuds.
print(len("Earbuds"))
5. Store how many products there are in a variable called count, then print it.
products = ["Earbuds", "Charger", "Phone case", "Screen guard"]
count = len(products)
print(count)

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed