Activity #3 – Strings and Lengths
Create a program that will accept an integer X from the user, followed by X
number of strings. The program will then print each string followed by the length of
each string. Then the program will print the shortest and the longest string.
Assume that each string has its own unique length.
Sample Run:
Sample Run A
Enter value of X: 3
Enter string value: a
Enter string value: ab
Enter string value: abc
a - total length is 1
ab - total length is 2
abc - total length is 3
The shortest string is a
The longest string is abc
Sample Run B
Enter value of X: 4
Enter string value: chico
Enter string value: strawberry
Enter string value: banana
Enter string value: guyabano
chico - total length is 5
strawberry - total length is 10
banana - total length is 6
guyabano - total length is 8
The shortest string is chico
The longest string is strawberry
x = int(input('Enter value of x:'))
str_values = []
for i in range(x):
print('Enter string value')
str = input()
str_values.append(str)
for s in str_values:
print(s, ' - total length is ', len(s))
str_values.sort(key=len)
print('The shortest string is ', str_values[0])
print('The longest string is ', str_values[-1])
Comments
Leave a comment