Single element from list –> save to variable –> len(variable) –> not 1?

Selecting List Elements I

employees = [‘Michael’, ‘Dwight’, ‘Jim’, ‘Pam’, ‘Ryan’, ‘Andy’, ‘Robert’]

index4 = employees[4]

print(index4)
–> “Ryan” # I was expecting this
print(len(index4))
–> 4 # I was expecting this to be 1?

my code

Hello.
The reason print(len(index4)) print 4 is this:

index4 = employees[4]#this now stores a string "Ryan".
print(len(index4)) #This print the length of the string, "Ryan". This is because
#index4 is the string "Ryan". Therefore, len(index4) just gets the length (4) or "Ryan".

I hope this helps!

2 Likes