{"id":14346,"date":"2021-09-30T12:30:05","date_gmt":"2021-09-30T07:00:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=14346"},"modified":"2021-11-22T18:34:38","modified_gmt":"2021-11-22T13:04:38","slug":"python-program-to-print-even-length-words-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-even-length-words-in-a-string\/","title":{"rendered":"Python Program to Print Even Length Words in a String"},"content":{"rendered":"
Given multiple strings separated by spaces, the task is to print even-length words in the given string in Python.<\/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 print even-length words in the given string in Python.<\/p>\n Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list<\/a> is mandatory.<\/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 Given multiple strings separated by spaces, the task is to print even-length words in the given string in Python. Examples: Example1: Input: Given String =’Hello Good morning this is BTechgeeks online programming platform’ Output: Even length words in the given string [ Hello Good morning this is BTechgeeks online programming platform ] are: Good this …<\/p>\nGiven String ='Hello Good morning this is BTechgeeks online programming platform'<\/pre>\n
Even length words in the given string [ Hello Good morning this is BTechgeeks online programming platform ] are:\r\nGood\r\nthis\r\nis\r\nBTechgeeks\r\nonline\r\nplatform<\/pre>\n
Given String='hello this is btechgeeks online coding platform for btech students'<\/pre>\n
Even length words in the given string [ hello this is btechgeeks online coding platform for btech students ] are:\r\nthis\r\nis\r\nbtechgeeks\r\nonline\r\ncoding\r\nplatform\r\nstudents<\/pre>\n
Program to Print Even Length Words 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\ngvnstrng = 'Hello Good morning this is BTechgeeks online programming platform'\r\nprint('Even length words in the given string [', gvnstrng, '] are:')\r\n# Convert this string into a list of words and\r\n# split them with spaces using split() and list() functions.\r\nstrngwordslist = list(gvnstrng.split())\r\n# Loop in the words list using For loop.\r\nfor strngword in strngwordslist:\r\n # Calculate the length of the word using the len() function.\r\n wordleng = len(strngword)\r\n # Check if the length of the word is even or not\r\n # using the If conditional Statement.\r\n if(wordleng % 2 == 0):\r\n # If it is true then print the word.\r\n print(strngword)\r\n<\/pre>\n
Even length words in the given string [ Hello Good morning this is BTechgeeks online programming platform ] are:\r\nGood\r\nthis\r\nis\r\nBTechgeeks\r\nonline\r\nplatform<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = input('Enter some random string = ')\r\nprint('Even length words in the given string [', gvnstrng, '] are:')\r\n# Convert this string into a list of words and\r\n# split them with spaces using split() and list() functions.\r\nstrngwordslist = list(gvnstrng.split())\r\n# Loop in the words list using For loop.\r\nfor strngword in strngwordslist:\r\n # Calculate the length of the word using the len() function.\r\n wordleng = len(strngword)\r\n # Check if the length of the word is even or not\r\n # using the If conditional Statement.\r\n if(wordleng % 2 == 0):\r\n # If it is true then print the word.\r\n print(strngword)\r\n<\/pre>\n
Enter some random string = hello this is btechgeeks online coding platform for btech students\r\nEven length words in the given string [ hello this is btechgeeks online coding platform for btech students ] are:\r\nthis\r\nis\r\nbtechgeeks\r\nonline\r\ncoding\r\nplatform\r\nstudents<\/pre>\n
\n