Find out the longest word | Sololearn: Learn to code for FREE!

-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

10/24/2020 3:35:21 PM

Joe Ma

80 Answers

New Answer

+66

text = input().split() length = [len(x) for x in text] maximum = max(length) text_index = length.index(maximum) print(text[text_index])

+45

This should solve it: txt = input().split() print(max(txt, key = len)) Cheers m8!

+27

https://code.sololearn.com/cJSlK0D24SPT/?ref=app You can get everything you need to know from here.

+21

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)

+10

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)

+8

txt = "this is an awesome text example" d = {i:len(i) for i in txt.split()} ml = max([d[i] for i in d]) print(d, end=2*'\n') print(f'max. number of characters: {ml}', end=2*'\n') for i in d: if d[i] == ml: print(i)

+6

The suggestion of Bahha🐧 prints only the first word with the max. length. This I demonstrate in the following example where the max number is 7 and this applies to the words "awesome" and "example": txt = "this is an awesome text example" k = txt.split() maxWordLength = max(len(word) for word in k) print(max(k, key=len))

+4

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

+3

Maxwell Anderson Yes, "fails" was not the correct word. I wanted to say that it does not give a complete answer. The answer is correct in parts. It prints only the first word which have the max. number of characters. But the later words of the same character count will be neglected.

+3

text = input().split() new = sorted(text, key=len) print(new[-1])

+2

Joe Ma , you can do it this way 🐱 https://code.sololearn.com/c7yXFPkjY4of/?ref=app

+2

print(max(k, key=len)) no need for a loop

+2

My attempt: txt = input() #your code goes here words = txt.split(" ") longest = "" for word in words: if len(word) > len(longest): longest = word print(longest) 1. make a list by splitting the txt 2. empty string longest 3. for every word in the words list, if the current word is longer than the current longest word (nothing at the beginning) set it to the current word 4. print longest word

+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.

+1

txt = input() #your code goes here def l(f): k=max(f,key=len) return k f=txt.split() print(l(f))

+1

Anyone send me c++ projects answers

+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?

+1

txt = input() #your code goes here list = txt.split(' ') word = "" for i in list: if len(i) > len(word): word = i print(word)

+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)])

+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)