Program to Read a List of Words and Return the Length of the Longest Word

Python Program to Read a List of Words and Return the Length of the Longest Word

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Given the list of words, the task is to write a python program that reads the list of words and prints the largest/longest words in the list.

Examples:

Example1:

Input:

given list of words = Hello this is BTechGeeks  online platform

Output:

Printing the longest length word :
BTechGeeks

Example2:

Input:

given list of words = Sky is pink

Output:

Printing the longest length word :
pink

Python Program to Read a List of Words and Return the Length of the Longest Word

There are several ways to read a list of words and return the length of the longest word some of them are:

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.

Method #1: Reading a list of words separated by a new line and comparing the length of words using if statement (User Input)

Approach:

  • Scan the number of elements/strings and store it in a variable
  • Using a for loop, accept the values into the list and put them into the list.
  • Assume that the first element is the one with the longest word.
  • The lengths of the words in the list are then compared with the first element using a for loop and an if statement.
  • In a temporary variable, save the name of the term with the longest length.
  • Print the word that has the longest length.
  • Exit of program

Below is the implementation:

# Taking a empty list
listWords = []
# scanning the number of strings
numb = int(input("enter the total number of strings :"))
# Using for loop
for i in range(numb):
    elem = input("enter some random string or word :\n")
    listWords.append(elem)
# taking the first element length as max length word
maxLength = len(listWords[0])
# Initializing a temp variable which stores the first word
word = listWords[0]
# Traversing the words list
for i in listWords:
    if(len(i) > maxLength):
        maxLength = len(i)
        word = i
print("Printing the longest length word :")
print(word)

Output:

enter the total number of strings :6
enter some random string or word :
hello
enter some random string or word :
this
enter some random string or word :
is
enter some random string or word :
BTechGeeks
enter some random string or word :
online
enter some random string or word :
platform
Printing the longest length word :
BTechGeeks

Method #2: Reading a list of words separated by space and comparing length of words using if statement (User Input)

Approach:

  • Scan the list of words separated by space.
  • Store the above string in the list using the split() function.
  • Assume that the first element is the one with the longest word.
  • The lengths of the words in the list are then compared with the first element using a for loop and an if statement.
  • In a temporary variable, save the name of the term with the longest length.
  • Print the word that has the longest length.
  • Exit of program

Below is the implementation:

# Taking a list of words in list using split
listWords = list(input("Enter the list of words / enter the sentence\n").split())
# taking the first element length as max length word
maxLength = len(listWords[0])
# Initializing a temp variable which stores the first word
word = listWords[0]
# Traversing the words list
for i in listWords:
    if(len(i) > maxLength):
        maxLength = len(i)
        word = i
print("Printing the longest length word :")
print(word)

Output:

Enter the list of words / enter the sentence
hello this is BTechGeeks online Platform
Printing the longest length word :
BTechGeeks

Method #3: Reading a list of words separated by space as static input and comparing length of words using if statement (Static Input)

Approach:

  • Give the list of words separated by space as static input.
  • Assume that the first element is the one with the longest word.
  • The lengths of the words in the list are then compared with the first element using a for loop and an if statement.
  • In a temporary variable, save the name of the term with the longest length.
  • Print the word that has the longest length.
  • Exit of program

Below is the implementation:

# Taking a list of words in list
listWords = ["Hello", "this", "is", "BTechGeeks", "online", "Platform"]
# taking the first element length as max length word
maxLength = len(listWords[0])
# Initializing a temp variable which stores the first word
word = listWords[0]
# Traversing the words list
for i in listWords:
    if(len(i) > maxLength):
        maxLength = len(i)
        word = i
print("Printing the longest length word :")
print(word)

Output:

Printing the longest length word :
BTechGeeks

Related Programs: