Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python program to find the longest word in a sentence

  • Difficulty Level : Basic
  • Last Updated : 10 Feb, 2021

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


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: S = “be confident and be yourself”
Output: “confident”
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(1)

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 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
# Driver Code
if __name__ == "__main__":
    # Given string
    s = "be confident and be yourself"
    # Split the string into words
    l = list(s.split(" "))



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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!