{"id":8394,"date":"2021-09-30T16:00:12","date_gmt":"2021-09-30T10:30:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=8394"},"modified":"2021-11-22T18:34:25","modified_gmt":"2021-11-22T13:04:25","slug":"python-program-to-read-a-list-of-words-and-return-the-length-of-the-longest-word","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-read-a-list-of-words-and-return-the-length-of-the-longest-word\/","title":{"rendered":"Python Program to Read a List of Words and Return the Length of the Longest Word"},"content":{"rendered":"
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<\/a> available.<\/p>\n 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.<\/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 There are several ways to read a list of words and return the length of the longest word some of them are:<\/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 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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ngiven list of words = Hello this is BTechGeeks\u00a0 online platform<\/pre>\n
Printing the longest length word :\r\nBTechGeeks<\/pre>\n
given list of words = Sky is pink<\/pre>\n
Printing the longest length word :\r\npink<\/pre>\n
Python Program to Read a List of Words and Return the Length of the Longest Word<\/h2>\n
\n
Method #1: Reading a list of words separated by a new line and comparing the length of words using if statement (User Input)<\/h3>\n
\n
# Taking a empty list\r\nlistWords = []\r\n# scanning the number of strings\r\nnumb = int(input(\"enter the total number of strings :\"))\r\n# Using for loop\r\nfor i in range(numb):\r\n elem = input(\"enter some random string or word :\\n\")\r\n listWords.append(elem)\r\n# taking the first element length as max length word\r\nmaxLength = 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) > maxLength):\r\n maxLength = len(i)\r\n word = i\r\nprint(\"Printing the longest length word :\")\r\nprint(word)\r\n<\/pre>\n
enter the total number of strings :6\r\nenter some random string or word :\r\nhello\r\nenter some random string or word :\r\nthis\r\nenter some random string or word :\r\nis\r\nenter some random string or word :\r\nBTechGeeks\r\nenter some random string or word :\r\nonline\r\nenter some random string or word :\r\nplatform\r\nPrinting the longest length word :\r\nBTechGeeks\r\n\r\n<\/pre>\n
Method #2: Reading a list of words separated by space and comparing length of words using if statement (User Input)<\/h3>\n
\n
# Taking a list of words in list using split\r\nlistWords = list(input(\"Enter the list of words \/ enter the sentence\\n\").split())\r\n# taking the first element length as max length word\r\nmaxLength = 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) > maxLength):\r\n maxLength = len(i)\r\n word = i\r\nprint(\"Printing the longest length word :\")\r\nprint(word)\r\n<\/pre>\n
Enter the list of words \/ enter the sentence\r\nhello this is BTechGeeks online Platform\r\nPrinting the longest length word :\r\nBTechGeeks\r\n\r\n<\/pre>\n
Method #3: Reading a list of words separated by space as static input and comparing length of words using if statement (Static Input)<\/h3>\n
\n
# Taking a list of words in list\r\nlistWords = [\"Hello\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"Platform\"]\r\n# taking the first element length as max length word\r\nmaxLength = 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) > maxLength):\r\n maxLength = len(i)\r\n word = i\r\nprint(\"Printing the longest length word :\")\r\nprint(word)\r\n<\/pre>\n
Printing the longest length word :\r\nBTechGeeks<\/pre>\n
\n
\n
\n