- 4
Given a text as input, find and output the longest word. Sample Input this is an awesome text Sample Output awesome
Longest word
6 ответов
+ 7
Atul ,
certain
as Denise Roßberg already mentioned: using split() is an easy and preferred way to split strings at a certain separator:
txt = 'this is my coding'
lst = txt.split() # ['this', 'is', 'my', 'coding']
for word in lst:
....
+ 6
Tia ,
as Atul already mentioned, you should show us your attempt first.
to give you some hints to start:
▪︎take an input
▪︎split the input at the position of spaces to a list
▪︎create a variables as buffer for the longest word and the length of it
▪︎iterate through the list, that gives you one word in each iteration
▪︎compare the word length given in the loop with the length in the buffer
▪︎if new word is longer than word in buffer, replace word and length in buffer
▪︎if iteration is terminated, the longest word is in the buffer.
▪︎print it out
+ 2
Post your code here
+ 2
Lothar do we need to split it into spaces or only using if will work?
+ 2
Atul Would be possible but the split function makes it easier.
+ 1
txt = input()
a= txt.split()
length=[len(i) for i in a]
maximum = max(length)
text_index = length.index(maximum)
print(a[text_index])
this code is wonderful :)