{"id":17595,"date":"2021-08-26T10:42:36","date_gmt":"2021-08-26T05:12:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=17595"},"modified":"2021-11-22T18:37:12","modified_gmt":"2021-11-22T13:07:12","slug":"python-program-to-find-longest-word-from-sentence","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-longest-word-from-sentence\/","title":{"rendered":"Python Program to Find Longest Word from Sentence"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Move all Negative Elements to End in Order with Extra Space Allowed<\/a> split() method:<\/strong><\/p>\n The split() method in Python divides a string into a list of strings by breaking the string with the specified separator.<\/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 longest word from a given sentence.<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Move all Negative Elements to End in Order with Extra Space Allowed Given a string and the task is to find the longest word from a given sentence. split() method: The split() method in Python divides a string into a list of strings by breaking …<\/p>\n
\nGiven a string and the task is to find the longest word from a given sentence.<\/p>\nGiven String = \"Hello this is btechgeeks\"<\/pre>\n
The Longest Word in the above given sentence = btechgeeks\r\nThe length of the longest word = 10<\/pre>\n
Given String = \"Goodmorning this is btechgeeks\"<\/pre>\n
The Longest Word in the above given sentence = Goodmorning\r\nThe length of the longest word = 11<\/pre>\n
Program to Find Longest Word from Sentence<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\nGvn_str = \"Hello this is btechgeeks\"\r\n# Split the given string into a list of words using the split() function and\r\n# store it in another variable say \"wrd_lst\".\r\nwrd_lst = Gvn_str.split()\r\n# Get the longest word from a given sentence using max(), key functions, and\r\n# store it in another variable.\r\nlongst_wrd = max(wrd_lst, key=len)\r\n# Calculate the length of the above-obtained longest word using the len() function\r\n# and store it in another variable.\r\nlen_longst_wrd = len(longst_wrd)\r\n# Print the longest word in the above-given sentence.\r\nprint(\"The Longest Word in the above given sentence = \", longst_wrd)\r\n# Print the above-obtained length of the longest word.\r\nprint(\"The length of the longest word = \", len_longst_wrd)\r\n<\/pre>\n
The Longest Word in the above given sentence = btechgeeks\r\nThe length of the longest word = 10<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\nGvn_str = input(\"Enter some random sentence = \")\r\n# Split the given string into a list of words using the split() function and\r\n# store it in another variable say \"wrd_lst\".\r\nwrd_lst = Gvn_str.split()\r\n# Get the longest word from a given sentence using max(), key functions, and\r\n# store it in another variable.\r\nlongst_wrd = max(wrd_lst, key=len)\r\n# Calculate the length of the above-obtained longest word using the len() function\r\n# and store it in another variable.\r\nlen_longst_wrd = len(longst_wrd)\r\n# Print the longest word in the above-given sentence.\r\nprint(\"The Longest Word in the above given sentence = \", longst_wrd)\r\n# Print the above-obtained length of the longest word.\r\nprint(\"The length of the longest word = \", len_longst_wrd)\r\n<\/pre>\n
Enter some random sentence = Goodmorning this is btechgeeks\r\nThe Longest Word in the above given sentence = Goodmorning\r\nThe length of the longest word = 11<\/pre>\n
\n