- 1

How I can output the longest word in text?

Using the text as input, find and output the longest word. Sample input this is an awesome text Result example awesome Remember the split ('') method, which returns a list of words in a string. https://code.sololearn.com/co74yVYF71R2/?ref=app

12th Dec 2020, 6:12 PM
Azizkxon Murotkhonov
Azizkxon Murotkhonov - avatar
8 ответов
+ 10
Azizkxon Murotkhonov , you could do like: - take input with input() function - separate the sentence from input to a list by using split() - sort the just created list by using a special argument for sorted that is a lambda function and sort by length of element - after this the last element in the list is the longest word. Happy coding!
12th Dec 2020, 6:23 PM
Lothar
Lothar - avatar
+ 4
x="this is an awesome text" y=x.split() k=max(len(p) for p in y) for i in y: if len(i)==k: print(i) >>>>>>awesome
17th Dec 2020, 6:40 PM
Hüseyin Emre Gözen
Hüseyin Emre Gözen - avatar
+ 3
Lothar Thanks, I have done this code
12th Dec 2020, 7:59 PM
Azizkxon Murotkhonov
Azizkxon Murotkhonov - avatar
+ 3
Hope this helps txt = input() #your code goes here longest = max(txt.split(), key=len) print( longest )
29th Apr 2021, 10:19 AM
Diana Ndinda
Diana Ndinda - avatar
+ 2
Using len function
13th Dec 2020, 8:22 AM
Sayyam Jain
Sayyam Jain - avatar
+ 2
I wrote a code that can output multiple strings, and is till beginner frendly (since im also a beginner): txt = input("str: ") splitted_txt = txt.split() char_numb = (len(p) for p in splitted_txt) max_char_numb = [max(char_numb)] for i in splitted_txt: if len(i) in max_char_numb: print(i)
30th Jan 2021, 1:47 PM
David Souza
David Souza - avatar
0
#Solution_1 text = input (" ").split(" ") text_dict = {} text_list = [] for word in text: text_dict[word] = len(word) text_list.append(len(word)) for key,value in text_dict.items(): if value == max(text_list): print (key) #Solution_2 text = input (" ").split(" ") text_dict = {} text_list = [] for word in text: text_dict[word] = len(word) max_value = 0 max_key = " " for key, value in text_dict.items(): if value > max_value: max_key = key max_value = value print(max_key)
6th Jan 2022, 5:28 AM
Zeus Hazroun
Zeus Hazroun - avatar
0
txt = input() #your code goes here y=txt.split(" ") k=max(len(p) for p in y) for i in y: if len(i)==k: print(i)
27th Jan 2022, 10:23 AM
Dev Priya Saha
Dev Priya Saha - avatar