Finding Array Length

Sometimes we don't know how many elements an array has (perhaps someone else created the array, or the number of elements in the array changes) and we need to find out.

For this, we use the len() function, which returns the number of elements in the array. The len() function is used similarly to the print() function, so we place the array whose length we want to find out in the parentheses.

Let's look at an example:

students = ["Jennifer", "Boris", "Nancy", "David", "Ava"]

If we want to find out how many students are in the students array, we can store the number of elements in a variable and print it:

number_of_students = len(students)
print(number_of_students)  # Prints: 5

Or print it directly:

print(len(students)) # Prints: 5

Instructions

Use the len() function to print the number of elements in the colleagues array.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/10