+ 3
Hello guys. In a given string, how can one fine and output the longest word by writing a program in python?
10 odpowiedzi
+ 9
You can search for codes in the Code section :
https://code.sololearn.com/cjClje60YyXV/?ref=app
+ 6
print([ st if len(st) == max(s.split(),key=len) for st in s.split()])
edit:
print([t for t in s.split() if len(t) == len(max(s.split(), key=len))])
+ 5
Jan Markus dss verwechselt ich immer
Thanks for showing correct solution.
+ 3
The clasic algorithmus Mas-ud Al-hassan for find out the maximum is to declare my first item as maximum and check items of the list. If an item will be greather then will be set to the maximum.
short solution for longest word:
https://code.sololearn.com/cd89N09f7P0R/?ref=app
+ 2
Have a look at the built-in max function and use key=len.
+ 1
@Jan Markus
True...but that is what was requested in the original post "longest word".....this would get multiple instances using the max and list comprehension:-
mystring = input()
mylist = [x for x in mystring.split() if len(x) == len(max(mystring.split(), key=len))]
print(*mylist)
0
you would use the len function in conjunction with the max function to get the answer. for strings it goes by capatilazation so be aware of that
0
or you could use the .split function and split at every instance of a space. Then split each word into an individual array or list. then find the word with the longest array and return that
0
This was my solution. There might be a better way, but this uses just the skills, functions, and methods taught up to that project:
https://code.sololearn.com/c3A23A20a8A3
- 2
Hf