Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # longest_word.py by gauravssnl
- # find longest word in a sentence
- def longest_word( sentence ) :
- # words are separated by white space ,so get words_list by using split function where white space is delimiter
- words_list = sentence.split( " " )
- # initially set result as first word in words_list
- res = words_list.pop(0)
- # now check other words and set result res to longer word
- for word in words_list :
- if len( word ) > len( res ) :
- res = word
- # change words_list to start from next index
- words_list = words_list[ 1 : ]
- return res
- if __name__ == '__main__' :
- print( longest_word( "She is Angela" ) )
- print( longest_word( "Python is amazing" ) )
- print ( longest_word( "A" ) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement