Python – Length of Range

To find the length of a range in Python, call len() builtin function and pass the range object as argument. len() function returns the number of items in the range.

Reference – Python len() builtin function

In the following example, we will take a range, and find its length using len() function.

Python Program

ADVERTISEMENT
rangeObject = range(2, 9)
length = len(rangeObject)
print(f'Length of this range is {length}.')
Try Online

Output

Length of this range is 7.

In the following example, we will take a range with a step value of 2, and find its length using len() function.

Python Program

rangeObject = range(2, 9, 2)
length = len(rangeObject)
print(f'Length of this range is {length}.')
Try Online

Output

Length of this range is 4.

Conclusion

In this Python Tutorial, we learned how to find the length of Python Range object using len() function, with example program.