0

Output the longest word, You mAy use Split.

txt = input() print(txt.split(" ")). I tried upto this. Not working

16th Feb 2021, 11:20 AM
We Doru
We Doru - avatar
11 Answers
+ 4
'''You can do it like this''' txt = input() wordslist = txt.split(" ") ''' This makes a list of the words''' longestword = "" for word in wordslist: if len(word) > len(longestword): longestword = word ''' This checks every word of the list and if it is longer than the selected word it overwrites it''' print (longestword) ''' https://code.sololearn.com/cynhXHT60v8F/?ref=app '''
16th Feb 2021, 12:28 PM
Ezequiel Lopez Saaied
Ezequiel Lopez Saaied - avatar
+ 3
Well now you need to compare the length of each word with other word like assuming, max=len(list_of_words[0]) if len(list_of_words[1]>max: max=len(list_of_words)
16th Feb 2021, 11:25 AM
Abhay
Abhay - avatar
16th Feb 2021, 11:50 AM
JaScript
JaScript - avatar
+ 1
max() sorts strings lexicographically by default. But it has an optional parameter key that you can use like this: max(input().split(), key=len) Detailed explanation: https://thepythonguru.com/JUMP_LINK__&&__python__&&__JUMP_LINK-builtin-functions/max/ https://www.programiz.com/python-programming/methods/built-in/max
16th Feb 2021, 12:05 PM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar
0
But how do I know how mAny Are there.
16th Feb 2021, 11:30 AM
We Doru
We Doru - avatar
0
First I need to turn the text into A list. Right. Then I would choose the mAx word using mAx().
16th Feb 2021, 11:35 AM
We Doru
We Doru - avatar
0
Wedo Ru by splitting string you can get the length of list which means how many words are there.
16th Feb 2021, 11:37 AM
Abhay
Abhay - avatar
0
txt=input (). And what follow next. Kindly, put the code in order.
16th Feb 2021, 11:39 AM
We Doru
We Doru - avatar
0
I tried print(max(txt.split(" "))), but it is giving me The Shortest word
16th Feb 2021, 11:43 AM
We Doru
We Doru - avatar
0
Yes it will, because you need the longest word (as i showed above how to do that), and max function compares the ascii value of each character for every word.
16th Feb 2021, 11:48 AM
Abhay
Abhay - avatar
0
txt = input() #your code goes here totaltxt = txt.split() longest = 0 for i in range(1, len(totaltxt)): if len(totaltxt[longest]) < len(totaltxt[i]): longest = i print(totaltxt[longest])
1st Mar 2022, 11:20 AM
Game0ver
Game0ver - avatar