Exercise 38 

Write a Python program to find the longest word on a string.

Solution:

def longestWord(s):
# Converting the string s to a list
L = s.split()
# define temporary longest word
maxWord = ""
# Browsing all items in list L
for word in L:
if (len(maxWord) < len(word)):
maxWord = word
return maxWord
# Testing algorithm
s = "Python is object oriented programming language"
print("The longest word in s is : " , longestWord(s))
# This will display : The longest word in s is : programming
Younes Derfoufi
my-courses.net

Leave a Reply