Find out the longest word | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Find out the longest word

I want to find out the longest word of a string. However it turnout a TypeError: 'int' object is not iterable. How can I correct the coding? Thanks https://code.sololearn.com/cbKwR5MGZsX5/?ref=app

24th Oct 2020, 3:35 PM
Joe Ma
Joe Ma - avatar
82 Answers
+ 79
text = input().split() length = [len(x) for x in text] maximum = max(length) text_index = length.index(maximum) print(text[text_index])
25th Oct 2020, 6:01 AM
Si Thu Tun
Si Thu Tun - avatar
+ 56
This should solve it: txt = input().split() print(max(txt, key = len)) Cheers m8!
26th Dec 2020, 4:42 PM
Deniz
Deniz - avatar
+ 27
https://code.sololearn.com/cJSlK0D24SPT/?ref=app You can get everything you need to know from here.
25th Oct 2020, 6:01 AM
Si Thu Tun
Si Thu Tun - avatar
+ 22
txt = input() #your code goes here txt_list = txt.split(" ") #print(txt_list) dictman= {i:len(i) for i in txt_list} maxman = max(dictman[i] for i in txt_list) for i in dictman: if dictman[i] == maxman: print(i)
2nd Nov 2020, 12:08 PM
Toy
+ 15
txt = "this is an awesome text" #your code goes here k = txt.split() j='' for i in k: if len(i) > len(j): j=i print(j)
25th Oct 2020, 6:22 AM
Crunk
Crunk - avatar
+ 7
text = input().split() new = sorted(text, key=len) print(new[-1])
24th May 2021, 5:57 AM
Xcalibur
Xcalibur - avatar
+ 3
print(max(k, key=len)) no need for a loop
24th Oct 2020, 3:48 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 3
I am still beginning and I was thinking about a cde that doesnt give an erorr if there are more than one longest words: 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) Please correct me if there is something wrong in the code. i ran it some times now and it worked well
30th Jan 2021, 1:46 PM
David Souza
David Souza - avatar
+ 3
txt = input() #your code goes here list = txt.split(' ') word = "" for i in list: if len(i) > len(word): word = i print(word)
7th Jun 2021, 12:56 PM
Arshak
Arshak - avatar
24th Oct 2020, 3:45 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
I don't see how Bahha🐧 solution fails, it prints out the longest word with or without multiple words of the same max length. Their answer is also the most concise.
24th Oct 2020, 4:08 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 1
txt = input() #your code goes here def l(f): k=max(f,key=len) return k f=txt.split() print(l(f))
10th Nov 2020, 3:37 PM
Rupan Dutta
Rupan Dutta - avatar
+ 1
Anyone send me c++ projects answers
12th Dec 2020, 4:47 PM
Navayug Sriram
+ 1
what's wrong with the below code? txt = input() Text=txt.split() #print(Text) count=0 maxvalue=0 for c in Text: if len(c) > maxvalue: maxvalue=len(c) #print(c) #print(maxvalue) count=count+1 if maxvalue==len(c): print(Text[maxvalue]) break Test case 1 is getting passed but two is not getting passed. Any idea to resolve this?
3rd Mar 2021, 4:33 AM
Ramakrishnan Mahesh
Ramakrishnan Mahesh - avatar
+ 1
This worked for me: txt = input() #your code goes here wnum=txt.split(" ") list=[] for w in wnum: list.append(len(w)) maxval=max(list) print(wnum[list.index(maxval)])
31st Jul 2021, 1:05 PM
Aynur Sunagatullin
Aynur Sunagatullin - avatar
+ 1
text=input() longest='' b=list(text.split(" ")) for i in range(len(b)): if len(b[i]) > len(longest): longest=b[i] print() print(longest)
16th Aug 2021, 1:34 PM
Bojanapu poojitha
Bojanapu poojitha - avatar
+ 1
txt = input() #your code goes here x = txt.split(" ") n = [len(i) for i in x] for i in x: if len(i) == max(n): print(i)
18th Sep 2021, 5:30 PM
Rayen Boussayed Mohammed Amin
Rayen Boussayed Mohammed Amin - avatar
+ 1
text = input().split() length = [len(x) for x in text] maximum = max(length) text_index = length.index(maximum) print(text[text_index])
14th Oct 2021, 3:38 PM
michael
michael - avatar
+ 1
You can see this video for the explanation with full code Video-> https://youtu.be/FZ0HoOv9rY4
12th Feb 2022, 9:48 AM
Divyanshu Pawar
Divyanshu Pawar - avatar
+ 1
txt=input() print(max(txt.split(), key=len))
19th Feb 2022, 9:24 PM
Alcino Castelo