This code is supposed to find the longest word in a string using the split function but it doesn't work with me HELP ! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

This code is supposed to find the longest word in a string using the split function but it doesn't work with me HELP !

txt = input() txt.split() txt[0]=max for i in len(txt): if len(txt[i])>max: max=txt[i] print(max)

8th Jan 2022, 7:07 PM
Wijdene Madiouni
Wijdene Madiouni - avatar
59 Answers
+ 20
txt = input() txt = txt.split() # Reassigning txt because split does not modify the original string max = len(txt[0]) longest = '' for i in range(len(txt)): if len(txt[i]) > max: max = len(txt[i]) longest = txt[i] print(longest)
8th Jan 2022, 10:31 PM
John
John - avatar
+ 11
By default, max() returns a value which has higher ASCII value. You can use key (optional argument of max() )to compare the strings by their length. print(max(txt.split(), key = len))
9th Jan 2022, 5:55 AM
Simba
Simba - avatar
+ 8
Brian John thank u a bunch guys ❤️
8th Jan 2022, 11:10 PM
Wijdene Madiouni
Wijdene Madiouni - avatar
+ 7
user sentence = input() longest = max(sentence.split(), key=len) print(longest)
9th Jan 2022, 6:17 PM
Mostafa Ahmad
Mostafa Ahmad - avatar
+ 5
This one hurt my confidence. I dont think we have learned the easier key=len thing either. txt = input() lst = txt.split(" ") long = 0 for w in lst: length=len(w) if length > long: long = length for word in lst: if len(word) == long: print(word)
9th Jan 2022, 9:42 AM
Travis Beard
Travis Beard - avatar
+ 4
In your code what is max? Is it length of word or str the word itself? If you write txt[0] = max and then use if statement like if len(txt[i])>max This make no sense.
8th Jan 2022, 9:22 PM
SoloilSole
SoloilSole - avatar
+ 4
Here is a definitive working version of your code. There were a couple tweaks beyond my listed changes. Usually I am more comprehensive, but I am healing from illness right now, so I apologize. txt = input() txt = txt.split() max=len(txt[0]) for i in txt: if len(i)>max: max=len(i) print(max)
8th Jan 2022, 10:19 PM
Brian
Brian - avatar
+ 4
Travis Beard it looks like after you save the length if the largest word, you loop over the original array again, and print every word equal to that max length. If multiple words are just as long, they also get printed again :)
9th Jan 2022, 10:19 AM
Tjipke van der Heide
Tjipke van der Heide - avatar
+ 3
Tjipke van der Heide in your last code, the print may output words that are less than max length, until that absolute max length is found. Edit: I see that is corrected now.
9th Jan 2022, 9:58 AM
Brian
Brian - avatar
+ 3
Brian only for a second ^^ I edited it quickly after posting :)
9th Jan 2022, 10:01 AM
Tjipke van der Heide
Tjipke van der Heide - avatar
+ 3
Wijdene Madiouni is because of speed? Hybridize it with Simba's suggestion that cuts out looping. Or is there an unmentioned other requirement, like filtering punctuation so it is not part of word length?
9th Jan 2022, 10:19 AM
Brian
Brian - avatar
+ 3
Travis Beard txt = input() lst = txt.split(" ") long = 0 #here u loop over lst for w in lst: length=len(w) if length > long: #if this word is bigger, update long long = length #this is a new loop over lst for word in lst: #if word is as long as long if len(word) == long: #print it print(word) Every word as long as long is printed here :) an alternative solution could be that, just like in Wijdene Madiouni 's code, you just save the longest word in the first loop, and just print that when the loops over. Then there's no need for a second loop, making it shorter and easier to understand :)
9th Jan 2022, 10:22 AM
Tjipke van der Heide
Tjipke van der Heide - avatar
+ 3
print(max({i:len(i) for i in input().split()})) This also returns longest word. Use this one line code.
9th Jan 2022, 5:34 PM
Anna Hari
Anna Hari - avatar
+ 2
Try this Example hope it work txt = input("Enter Text:").split() longest = "" for i in txt: if len(i) > len(longest): longest = i print(longest)
9th Jan 2022, 7:47 AM
Code_lover
Code_lover - avatar
+ 2
Try this: txt = input() txt.split() txt[0]=max for i in txt: if len(txt[i])>max: max=len(txt[i]) print(max)
9th Jan 2022, 8:51 AM
Attique ur rehman
Attique ur rehman - avatar
+ 2
Again, here is Simba's elegant solution print(max(input().split(), key=len) Here is a fast hybrid that prints all words that are max length. txt = input().split() mx=len(max(txt, key=len)) print(list(filter(lambda w: len(w)==mx, txt))) Use this last line replacement to print only the words on separate lines: print(*list(filter(lambda w: len(w)==mx, txt)), sep='\n')
9th Jan 2022, 10:40 AM
Brian
Brian - avatar
+ 2
"Again, here is Simba's elegant solution print(max(input().split(), key=len)" ^ I started out in that direction but was just using len I think (haven't learned key=len) and it was giving me the word with the highest "letter value" 😕 that's definitely my favorite way though. Brian
9th Jan 2022, 10:45 AM
Travis Beard
Travis Beard - avatar
+ 2
Wijdene Madiouni I see that you joined only 9 days ago. (Welcome!). DM may get enabled only after 21 days, though you can receive.
9th Jan 2022, 11:04 AM
Brian
Brian - avatar
+ 2
Brian okay i got u
9th Jan 2022, 11:11 AM
Wijdene Madiouni
Wijdene Madiouni - avatar
+ 1
Sorry for annoying u but can u write the whole code
8th Jan 2022, 10:08 PM
Wijdene Madiouni
Wijdene Madiouni - avatar