Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to find the longest word in a sentence

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 02 Feb, 2023
Improve Article
Save Article

Given a string S consisting of lowercase English alphabets, the task is to print the longest word present in the given string in python.

Examples:

Input: S = “be confident and be yourself”
Output: “confident”
Explanation:
Words present in the sentence are “be”, “confident”, “and”, “be” and “yourself”. Length of each of the words are 2, 9, 3, 2, and 8 respectively.
Therefore, the longest word is “confident”.

Input: S = “geeks for geeks”
Output: “geeks”

Searching-based Approach: Refer to this article to solve this problem by performing the following steps:

  • Iterate over the characters of the string.
  • Check if the current character encountered is a space or end of the string is reached.
  • If the current character is found to be so, update the maximum length of a word obtained.
  • Finally, print the longest word obtained using substr() method.

Time Complexity: O(N) 
Auxiliary Space: O(N)

Approach using sorted() method: The idea is to split the string into separate words and store in a list. Then, sort the list in the order of increasing length using the sorted() method. Print the last string in the sorted list.

Below is the implementation of the above approach:

Python3




# Python3 program for the above approach
 
# Function to print the longest
# word in given sentence
def largestWord(s):
 
    # Sort the words in increasing
    # order of their lengths
    s = sorted(s, key = len)
 
    # Print last word
    print(s[-1])
 
 
# Driver Code
if __name__ == "__main__":
 
    # Given string
    s = "be confident and be yourself"
 
    # Split the string into words
    l = list(s.split(" "))
 
    largestWord(l)

Output

confident

Time Complexity: O(N * logN)
Auxiliary Space: O(1)

Approach using max() and split() methods:  This approach uses the max() function with the split () method to split the string into a list of words and find the longest word.

Python3




# Python3 program for the above approach
 
# Function to print the
# longest word in the string s
 
 
def longestword(s):
     
    #splitting the string into words
    s=s.split()
    # Using max() method
    print(max(s,key=len))
 
 
# Driver Code
if __name__ == "__main__":
 
    # Given string
    s = "be confident and be yourself"
 
 
    #calling longestword function
    longestword(s)
#This code contributed by tvsk

Output

confident

Time Complexity: O(N), N is the number of words in the input string used for iteration only once.
Auxiliary Space: O(N), N is used for creating a new list from the input string and stores it in memory.

Approach using re module:

Python3




import re
 
def longest_word(sentence):
    words = re.findall(r'\b\w+\b', sentence) # find all words in the sentence using regular expression
    return max(words, key=len) # return the word with the maximum length
 
sentence = "be confident and be yourself"
print(longest_word(sentence))

Output

confident

Time Complexity: O(N), N is the number of words in the input string used for iteration only once.
Auxiliary Space: O(N), N is used for creating a new list from the input string and stores it in memory.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!