Python Program to print the length of an array


May 9, 2022, Learn eTutorial
953

In this simple python program, we need to find the length of an array. It's an intermediate-level python program.

To understand this example, you should have knowledge of the following Python programming topics:

How to find the length of an array?

An array is a collection of elements of the same data type. In this array python program, we need to find the number of elements in an array. For example, consider an array A. with the values [1. 2. 3. 4. 5]In this simple python program, we need to count the number of elements and print the count as 5 using print in python programming functions. In python language, it supports functions or methods called len() , which is a built-in method to find the length of the array. So we have to use that python method to find the length of the given array in the python language.

ALGORITHM

STEP 1: Initialize an array with some predefined values.

STEP 2: Use the print statement in the python programming language to print the number of elements in the array.

We use the len() method, a built-in method in python, to get the count of the array elements. We also use the str() function to convert the integer to string before printing the value.

Python Source Code

                                               
arr = [1, 2, 3, 4, 5];     
     
print("Number of elements in the array: " + str(len(arr)));     
                                      

OUTPUT

Number of elements in the array: 5