Program to Find Longest Word from Sentence

Python Program to Find Longest Word from Sentence

In the previous article, we have discussed Python Program to Move all Negative Elements to End in Order with Extra Space Allowed
Given a string and the task is to find the longest word from a given sentence.

split() method:

The split() method in Python divides a string into a list of strings by breaking the string with the specified separator.

Examples:

Example1:

Input:

Given String = "Hello this is btechgeeks"

Output:

The Longest Word in the above given sentence = btechgeeks
The length of the longest word = 10

Example2:

Input:

Given String = "Goodmorning this is btechgeeks"

Output:

The Longest Word in the above given sentence = Goodmorning
The length of the longest word = 11

Program to Find Longest Word from Sentence

Below are the ways to find the longest word from a given sentence.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Split the given string into a list of words using the split() function and store it in another variable say “wrd_lst”.
  • Get the longest word from a given sentence using max(), key functions, and store it in another variable.
  • Calculate the length of the above-obtained longest word using the len() function and store it in another variable.
  • Print the longest word in the above-given sentence.
  • Print the above-obtained length of the longest word.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
Gvn_str = "Hello this is btechgeeks"
# Split the given string into a list of words using the split() function and
# store it in another variable say "wrd_lst".
wrd_lst = Gvn_str.split()
# Get the longest word from a given sentence using max(), key functions, and
# store it in another variable.
longst_wrd = max(wrd_lst, key=len)
# Calculate the length of the above-obtained longest word using the len() function
# and store it in another variable.
len_longst_wrd = len(longst_wrd)
# Print the longest word in the above-given sentence.
print("The Longest Word in the above given sentence = ", longst_wrd)
# Print the above-obtained length of the longest word.
print("The length of the longest word = ", len_longst_wrd)

Output:

The Longest Word in the above given sentence =  btechgeeks
The length of the longest word =  10

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Split the given string into a list of words using the split() function and store it in another variable say “wrd_lst”.
  • Get the longest word from a given sentence using max(), key functions, and store it in another variable.
  • Calculate the length of the above-obtained longest word using the len() function and store it in another variable.
  • Print the longest word in the above-given sentence.
  • Print the above-obtained length of the longest word.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
Gvn_str = input("Enter some random sentence = ")
# Split the given string into a list of words using the split() function and
# store it in another variable say "wrd_lst".
wrd_lst = Gvn_str.split()
# Get the longest word from a given sentence using max(), key functions, and
# store it in another variable.
longst_wrd = max(wrd_lst, key=len)
# Calculate the length of the above-obtained longest word using the len() function
# and store it in another variable.
len_longst_wrd = len(longst_wrd)
# Print the longest word in the above-given sentence.
print("The Longest Word in the above given sentence = ", longst_wrd)
# Print the above-obtained length of the longest word.
print("The length of the longest word = ", len_longst_wrd)

Output:

Enter some random sentence = Goodmorning this is btechgeeks
The Longest Word in the above given sentence = Goodmorning
The length of the longest word = 11

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.