Given a text as input, find and output the longest word. Sample Input this is an awesome text Sample Output awesome | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

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

Please help me What is wrong? txt = input() #your code goes here list=f"{txt}".split(" ") print (max(list))

4th Nov 2021, 8:49 AM
G'anijonov Sarvarbek
G'anijonov Sarvarbek - avatar
8 Answers
+ 17
max() can find the longest word and is working properly with strings <<< (Slick) max can take a key argument, that has to be a function. iterable (list of strings) are parsed and comparison is based on its return value from the function that is used. in our case it has to be 'len': a = input().split() print(max(a, key = len))
4th Nov 2021, 2:38 PM
Lothar
Lothar - avatar
+ 3
max() doesn't work like that for characters. You'd need to get the length of each word and compare them. Then print the word with the largest length Lothar https://code.sololearn.com/c1jh6DDa5Ug6/?ref=app
4th Nov 2021, 8:54 AM
Slick
Slick - avatar
+ 3
dic={len(w):w for w in txt.split()} #find Max key and get its item # problems: u only last longest word.
4th Nov 2021, 9:14 AM
Oma Falk
Oma Falk - avatar
+ 3
txt = input(); #tu código va aquí print(max(txt.split(), key = len));
22nd Dec 2022, 12:46 AM
Ulises
Ulises - avatar
+ 3
txt = input() #your code goes here list= txt.split(" ") longest= max(list,key=len) print (longest)
1st Jan 2023, 3:22 PM
Arpita Karmakar
Arpita Karmakar - avatar
+ 2
Undertandable thanks
4th Nov 2021, 9:26 AM
G'anijonov Sarvarbek
G'anijonov Sarvarbek - avatar
+ 1
My code is: txt = input() #List of words string = txt.split(' ') # print(string) #Qty of strings # print(range(len(string))) #List of each word length q = [len(string[i]) for i in range(len(string))] # print(q) #Index of the longest word # print(q.index(max(q))) ilw = q.index(max(q)) #The longest word print(string[ilw])
11th Jul 2022, 12:09 PM
Pavel Ripyuk
Pavel Ripyuk - avatar
+ 1
My code is: txt=input() i=" " for w in txt.split(): #Here it is checked each word in the splitted word from txt-(input) if (len(w)>len(i)): #It's checked if the length of the word is bigger than the previous one i=w #If the value is bigger in letters, the value is replaced by the highest one in the variable i print (i) #The highest value word is returned Hope it helps up!
12th Jan 2023, 2:26 PM
Albert