+ 2
How to use Len for python
4 Answers
+ 29
The len() function can be used to find the length of an iterable object.
For example:
string_a = "I love Sololearn!"
print(len(string_a))
dict_a = {'a':1, 'b':2, 'c':3}
print(len(dict_a))
list_a = [0,1,2,3,4]
print(len(list_a))
tuple_a = ((0,1),(1,2),(3,4))
print(len(tuple_a))
You can use it for various things, like for dynamic input to set max, or min lengths for an iteration. Here is a short example:
max = len(string_a)
for character in range(0, max):
print(string_a[character])
+ 5
Do you mean the Len function? If so, simply insert a value : Len ("Hello") -This would print out: 5, as there are 5 letters in the word "Hello". The Len function gets the length of the assigned value.
+ 3
If you do len() on a list:
n = [1,2,3,10]
print(len(n))
it will print 4 (number of elements)
+ 2
since it is covered thuroughly, here is 1 more use..
myList=[1,2,3,4]
for i in range(len(myList)):
//do something
that will run a for loop 4 times because the for list len is 4