- 4

Given a text as input, find and output the longest word. Sample Input this is an awesome text Sample Output awesome

Longest word

6th Jun 2021, 3:38 PM
Tia
6 odpowiedzi
+ 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: ....
6th Jun 2021, 7:57 PM
Lothar
Lothar - avatar
+ 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
6th Jun 2021, 5:14 PM
Lothar
Lothar - avatar
+ 2
Post your code here
6th Jun 2021, 3:54 PM
Atul [Inactive]
+ 2
Lothar do we need to split it into spaces or only using if will work?
6th Jun 2021, 5:21 PM
Atul [Inactive]
+ 2
Atul Would be possible but the split function makes it easier.
6th Jun 2021, 5:29 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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 :)
4th Aug 2021, 5:17 PM
Motahare Soltani
Motahare Soltani - avatar