0

Hello guys right now I am kinda of new in python my question is related to the function call len() len() its print out the character but here it's the confusing part, I am working with a list let me explain an example players ['lee', 'bob', 'moe', 'joe'] len(players) 4 why it's giving me 4 item in the list I thought len it counts the characters and if I do this it give me the correct somewhat answer what I am seeking, len(players [-1]) 3

4th Jul 2016, 7:06 PM
Talha Khan
Talha Khan - avatar
8 Respuestas
+ 1
It's giving you 4 because your asking for the length of how many players are in your list, which are 4 players. To find the length of a player's name do: players = ['lee', 'bob', 'moe', 'joe'] print(len(players[0])) "[0]" is player "lee". "[1]" would be "bob".
4th Jul 2016, 7:59 PM
Dean
+ 1
ok got it thanks in a list it basically counts as an item not characters
4th Jul 2016, 8:22 PM
Talha Khan
Talha Khan - avatar
+ 1
len() is a function of both list and string when u do len(l) it counts number of elements in the list when u do len(l[1]) it counts the letters of the of the word at the index 1 and u are using negative index negative index are used to access the list from the back so in ur case there are 4 players and Len of Joe is 3 note if u put a number or float instead of string so u will get an error in the 2nd case
6th Jul 2016, 6:09 PM
Vansh Batra
Vansh Batra - avatar
+ 1
yes
6th Jul 2016, 6:14 PM
Vansh Batra
Vansh Batra - avatar
0
len (players) will return with the number of items in your list, because len will count the 'Length' of your list. print (players [1]) will return "bob" etc.
4th Jul 2016, 8:16 PM
Rob Jones
Rob Jones - avatar
0
You are writing about characters. I wonder if you the length of the list or the length of the strings. Because python can treat strings as lists. Please clarify yourself, if you need more help.
5th Jul 2016, 7:20 AM
Jurgen Mueller
Jurgen Mueller - avatar
0
len() counts items in it,not charecters
5th Jul 2016, 9:38 AM
abhilash
abhilash - avatar
0
len(players) would only give you the number of items in the list and not the number of characters. Also when you give negative numbers as index numbers it will count from the back. So if you typed print(players[-1]) it would print 'moe'.
8th Jul 2016, 5:43 PM
Athreyan MKS
Athreyan MKS - avatar