Len() vs range()?

Hello!

I am not understanding the functional difference between using len() and range(). For instance, in the code below I originally had for i in len(user_name) where for i in range(0, len(user_name) is.
Why is len() contained within range(), rather than simply using len()?

Thanks in advance!

The built in function range() will perform an action a specific number of times. Its parameters are start, stop, step.

the len() function returns the number of items in the length of an object.

https://docs.python.org/3/library/functions.html?highlight=len#len

In that particular code example, you’re starting at index 0 and going the length of the user_name. (The range of integers stops at -1.)

more on range():
https://www.geeksforgeeks.org/python-range-function/

2 Likes

Thank you for your response and the links!

1 Like