{"id":20317,"date":"2021-09-09T19:57:50","date_gmt":"2021-09-09T14:27:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=20317"},"modified":"2021-11-22T18:36:23","modified_gmt":"2021-11-22T13:06:23","slug":"python-program-to-find-the-shortest-word-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-shortest-word-in-a-string\/","title":{"rendered":"Python Program to Find the Shortest Word in a String"},"content":{"rendered":"
In the previous article, we have discussed Program to Print First 50 Natural Numbers Using Recursion<\/a><\/p>\n Given a string and the task is to find the shortest word in a given string.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the shortest word in a given string:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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<\/a>. Using them you can develop code on your own and master coding skills.<\/p>\n 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 …<\/p>\nGiven String = \"Hello this is BTechGeeks online Platform\"<\/pre>\n
Printing the shortest length word :\r\nis<\/pre>\n
Given String = \"good morning btechgeeks\"<\/pre>\n
Printing the shortest length word :\r\ngood<\/pre>\n
Program to Find the Shortest Word in a String in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str=\"Hello this is BTechGeeks online Platform \"\r\n# Convert the given string into a list of words using the list(),split() functions and\r\n#store it in a variable.\r\nlistWords = list(gvn_str.split())\r\n# taking the first element length as min length word\r\nminLength = len(listWords[0])\r\n# Initializing a temp variable which stores the first word\r\nword = listWords[0]\r\n# Traversing the words list\r\nfor i in listWords:\r\n if(len(i) < minLength):\r\n minLength = len(i)\r\n word = i\r\nprint(\"Printing the shortest length word :\")\r\nprint(word)<\/pre>\n
Printing the shortest length word :\r\nis<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function \r\n# and store it in a variable.\r\ngvn_str= input(\"Enter some random String = \")\r\n# Convert the given string into a list of words using the list(),split() functions and\r\n#store it in a variable.\r\nlistWords = list(gvn_str.split())\r\n# taking the first element length as min length word\r\nminLength = len(listWords[0])\r\n# Initializing a temp variable which stores the first word\r\nword = listWords[0]\r\n# Traversing the words list\r\nfor i in listWords:\r\n if(len(i) < minLength):\r\n minLength = len(i)\r\n word = i\r\nprint(\"Printing the shortest length word :\")\r\nprint(word)<\/pre>\n
Enter some random String = good morning btechgeeks\r\nPrinting the shortest length word :\r\ngood<\/pre>\n
\n