Exercise 19

Write Python algorithm that lists the strings in the list l = ["laptop", "iphone", "tablet", "printer", "Ipad"] by indicating the length of each string.

Solution

l = ["laptop", "iphone", "tablet", "printer", "Ipad"]

for s in l:
    print(s, ":", len(s))

"""
output:
laptop : 6
iphone : 6
tablet : 6
printer : 7
Ipad : 4
"""





In this program:

  1. We loops: through each string in the list l
  2. And we prints the string: followed by a colon and the length of the string.
  3. The length: of each string is calculated using the built-in len() function in Python.

 

Younes Derfoufi
CRMEF OUJDA

One thought on “Solution Exercise 19: python algorithm to list all string in given list”

Leave a Reply