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:
- We loops: through each string in the list l
- And we prints the string: followed by a colon and the length of the string.
- The length: of each string is calculated using the built-in len() function in Python.
Younes Derfoufi
CRMEF OUJDA
[…] Exercise 19 || Solution […]