Python Program to Find the Longest Word

Python Program to Find the Longest Word
Reading Time: 6 minutes


The provided Python program is designed to find the longest word(s) in a given input sentence. It prompts the user to enter a sentence, processes the input, and then determines the longest word(s) along with their length(s) before displaying the results.


  1. The program starts by defining a function named find_longest_words(input_string), which takes a single argument input_string representing the user’s input sentence.
  2. Inside the function, the input sentence is split into individual words using the split() method, which separates words based on spaces.
  3. Next, the program finds the length of the longest word(s) in the sentence using the max() function along with a generator expression that calculates the length of each word in the words list.
  4. After determining the maximum word length, the program filters out and collects all the words that have the same length as the longest word(s) using a list comprehension.
  5. The function returns a tuple containing the list of longest words and the maximum word length.
  6. The main part of the program checks if it is being run as the main module using the __name__ variable.
  7. If it is the main module, the program prompts the user to enter a sentence using the input() function and stores the input in the variable user_input.
  8. It then calls the find_longest_words() function with user_input as the argument and stores the returned values in longest_words and max_length variables.
  9. The program checks if there are any longest words found (longest_words list is not empty).
  10. If there is only one longest word, the program displays it along with its length using string formatting.
  11. If there are multiple longest words, the program displays them separated by commas and also shows the maximum word length.
  12. If no words are found in the input (e.g., if the user enters an empty sentence), the program prints a message indicating that no words were found.

Python Program to find longest name :

     def find_longest_words(input_string):
    # Split the input string into words
    words = input_string.split()
    # Find the length of the longest word(s)
    max_length = max(len(word) for word in words)
    # Filter and collect the longest word(s)
    longest_words = [word for word in words if len(word) == max_length]
    return longest_words, max_length
if __name__ == "__main__":
    user_input = input("Enter a sentence: ")
    longest_words, max_length = find_longest_words(user_input)

    if longest_words:
        if len(longest_words) == 1:
            print(f"The longest word is: {longest_words[0]}")
            print(f"The longest words are: {', '.join(longest_words)}")
        print(f"The length of the longest word(s) is: {max_length}")
        print("No words found in the input.")


Input :

     Enter a sentence: systech software solution

Output :

     The longest words are: software, solution
The length of the longest word(s) is: 8


The program utilizes the split() method to convert the input sentence into a list of words, and then it calculates the length of each word using a generator expression within the max() function. By finding the maximum word length, it can filter out all words that match this length and return them along with the maximum word length. The main part of the program handles user input and output, displaying the longest word(s) and their length(s) accordingly.