find the longest word in the string in python by R4R Team

Example-

string is "This is the string"
here longest word is "String"


program-

import functools

print("Enter string")
s=input()

l=s.split()

def func(x,y):
if len(x)>len(y):
return x
else:
return y

x=functools.reduce(func,l)
print("longest Word is :",x)


output-

Enter string
This is the string
longest Word is : string

Enter string
one two three four five six
longest Word is : three


-In this program, we take a input of string then we split this string into the list, then we use reduce() function which check two words by func() function and return max length word, and at last we have a one word with maximum length.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!