Find length of a Range


Length of a Range

To find the length of a range in Python, you can use len() built-in function. Call the len() function, and pass the range object as argument. The function returns an integer representing the number of items in the range.

Examples

1. Length of a range with start and stop

In the following program, we take a range defined by a specific start and stop values. And then we use len() function to get the length of this range object.

Python Program

myrange = range(0, 5)
print(len(myrange))

Output

5

2. Length of a range with a step value

In the following program, we find the length of a range object, where the range is defined by a specific start, stop, and step values.

Python Program

myrange = range(0, 20, 3)
print(len(myrange))

Output

7

Summary

In this tutorial of Python Ranges, we learned how to find the length of a given range using len() built-in function.