1

I was asked to solve a problem like this, but I found it a little bit difficult, as I am beginner in Python :

"Write code that uses iteration to print out the length of each element of the list stored in str_list."

str_list = ["hello", "", "goodbye", "wonderful", "I love Python"]

and I answered in a long silly way :

count = 0

for g in str_list[0] :
    count = count + 1
print(count)

count = 0

for g in str_list[1]:
    count = count + 1
print(count)   

count = 0

for g in str_list[2]:
    count = count + 1
print(count)   

count = 0

for g in str_list[3]:
    count = count + 1
print(count) 

count = 0

for g in str_list[4]:
    count = count + 1
print(count)

Please could you help me find a short summarized way to solve that problem.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    You are not really far. As you have already used `for` loop to count the length of each word, you can do the same for a each word now. If you look at all your blocks, they are closely the same. Just the index changes. You can summarise the`for` blocks in a big `for` loop (and iterate over each element in the list). – Alexandre B. Jul 06 '19 at 18:46
  • I think you are looking for nested for loop: https://stackoverflow.com/questions/24591917/nested-loop-python – Masoud Jul 06 '19 at 18:54
  • Please next time see who make the first correct answer it may that others takes some idea from it then post there answers i say maybe so give evrey one his right to emprove this comunity , thanks a lot – Ghassen Jul 06 '19 at 19:15

5 Answers5

3

For finding the length of every element, you can write this code:

str_list = ["hello", "", "goodbye", "wonderful", "I love Python"]
for l in str_list:
    print("Length of {} is: ".format(l),len(l))
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
Muhammad Hammad
  • 183
  • 1
  • 11
1

If you do want/need to compute the length yourself, here's how to do so without duplicating that code multiple times:

str_list = ["hello", "", "goodbye", "wonderful", "I love Python"]

for str in str_list:
    count = 0
    for g in str:
        count = count + 1
    print(count)

But if you can use len(), why reinvent the wheel. Just giving you another option.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Thanks a lot .Please could you clarify to me why you put " count = 0 " on line 4 ? And can we have it on line 2, before the for loop ? I thank you in advance ! – BeginnerProgrammer Jul 07 '19 at 20:25
  • You have to put 'count = 0' where it is so that it resets each time you start to compute the length of a new string. Then you'll loop over that string and increment count for each character in the string to arrive at the final count for THAT string. If you put it on line 2, it would never be reset, because it's outside and before the loop. Then, each number printed would be bigger than the prior one and the final number printed would be the total number of characters in all the strings. Try it for yourself. That's part of what makes programming fun. Things like that are easy to try. – CryptoFool Jul 07 '19 at 22:34
  • Thank you so much Steve ! You're a life saver ! – BeginnerProgrammer Jul 08 '19 at 19:51
0

Python has function called len() which calculates length of string. Check this out https://www.jquery-az.com/python/demo.php?ex=176.0_2

Swapnil
  • 125
  • 1
  • 9
0

You can use the len(string) function. It returns the length of the string.

It takes the string as the parameter.

It returns an integer which is the length of the string.

bart
  • 1,003
  • 11
  • 24
0

using this one line of code you can get the length of each element of the array in a list

l = [len(x) for x in str_list]
print(l)

output : [5, 0, 7, 9, 13]