Computer Science, asked by ayushgupta1933, 1 year ago

Write a python pgm to accept sentace from the user and display the largest word of sentancr along with its length

Answers

Answered by fiercespartan
4

As we are taking a sentence as our input, we will need to break the sentence down and find the length.

sentence = input('Enter the sentence')

Now let's split the sentence into different words using .split()

sentence = sentence.split()

We will first take the longest length as the length of the first word and check if there are any words with greater lengths. If there are, we will change the longest word and the length.

CODE:

sentence = input('Enter the sentence').split()

longest_word, length = sentence[0], len(sentence[0])

for word in sentence:

   if len(word) > length:

       longest_word, length = word, len(word)

print(f'Longest word is: {longest_word} and its length is {length}')

Similar questions