Program to Find the Shortest Word in a String

Python Program to Find the Shortest Word in a String

In the previous article, we have discussed Program to Print First 50 Natural Numbers Using Recursion

Given a string and the task is to find the shortest word in a given string.

Examples:

Example1:

Input:

Given String = "Hello this is BTechGeeks online Platform"

Output:

Printing the shortest length word :
is

Example2:

Input:

Given String = "good morning btechgeeks"

Output:

Printing the shortest length word :
good

Program to Find the Shortest Word in a String in Python

Below are the ways to find the shortest word in a given string:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Convert the given string into a list of words using the list() and split() functions and store it in a variable.
  • Assume that the first element is the one with the shortest 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 shortest length.
  • Print the word that has the shortest length.
  • The Exit of program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str="Hello this is BTechGeeks online Platform "
# Convert the given string into a list of words using the list(),split() functions and
#store it in a variable.
listWords = list(gvn_str.split())
# taking the first element length as min length word
minLength = 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) < minLength):
        minLength = len(i)
        word = i
print("Printing the shortest length word :")
print(word)

Output:

Printing the shortest length word :
is

Method #2: Using For loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Convert the given string into a list of words using the list() and split() functions and store it in a variable.
  • Assume that the first element is the one with the shortest 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 shortest length.
  • Print the word that has the shortest length.
  • The Exit of 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 String = ")
# Convert the given string into a list of words using the list(),split() functions and
#store it in a variable.
listWords = list(gvn_str.split())
# taking the first element length as min length word
minLength = 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) < minLength):
        minLength = len(i)
        word = i
print("Printing the shortest length word :")
print(word)

Output:

Enter some random String = good morning btechgeeks
Printing the shortest length word :
good

The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.