Print the longest word in a sentence

sravan953 0 Tallied Votes 513 Views Share

A program which accepts a sentence from a user, and uses a 'for loop' and determines the longest word in the sentence entered.

# Accept a sentence
str=raw_input("Enter a sentence: ")

# Split the sentence at each space
split_str=str.split()

p=0
s=""

for item in split_str:
    if(len(item)>p):
        s=item
        p=len(item)

print("Longest word: "+s)