KnowledgeBoat Logo

Computer Science

Write programs as per following specifications:

'''Print the length of the longest
string in the list of strings str_list.
Precondition : the list will contain
at least one element.'''

Python

Python List Manipulation

15 Likes

Answer

l = eval(input("Enter list of strings: "))
largeIdx = 0
largeLen = 0

for i in range(len(l)):
    length = len(l[i])
    if length > largeLen:
        largeLen = length
        largeIdx = i

print("Longest String:", l[largeIdx])

Output

Enter list of strings: ["apple", "orange", "pear", "strawberry", "kiwi"] 
Longest String: strawberry

Answered By

5 Likes


Related Questions