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 is BTechgeeks online platform
Example2:
Input:
Given String='hello this is btechgeeks online coding platform for btech students'
Output:
Even length words in the given string [ hello this is btechgeeks online coding platform for btech students ] are: this is btechgeeks online coding platform students
Program to Print Even Length Words in a String in Python
Below are the ways to print even-length words in the given string in Python.
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Method #1: Using For Loop (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Convert this string into a list of words and split them with spaces using split() and list() functions.
- Loop in the words list using For loop.
- Calculate the length of the word using the len() function.
- Check if the length of the word is even or not using the If conditional Statement.
- If it is true then print the word.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvnstrng = 'Hello Good morning this is BTechgeeks online programming platform' print('Even length words in the given string [', gvnstrng, '] are:') # Convert this string into a list of words and # split them with spaces using split() and list() functions. strngwordslist = list(gvnstrng.split()) # Loop in the words list using For loop. for strngword in strngwordslist: # Calculate the length of the word using the len() function. wordleng = len(strngword) # Check if the length of the word is even or not # using the If conditional Statement. if(wordleng % 2 == 0): # If it is true then print the word. print(strngword)
Output:
Even length words in the given string [ Hello Good morning this is BTechgeeks online programming platform ] are: Good this is BTechgeeks online platform
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 this string into a list of words and split them with spaces using split() and list() functions.
- Loop in the words list using For loop.
- Calculate the length of the word using the len() function.
- Check if the length of the word is even or not using the If conditional Statement.
- If it is true then print the word.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvnstrng = input('Enter some random string = ') print('Even length words in the given string [', gvnstrng, '] are:') # Convert this string into a list of words and # split them with spaces using split() and list() functions. strngwordslist = list(gvnstrng.split()) # Loop in the words list using For loop. for strngword in strngwordslist: # Calculate the length of the word using the len() function. wordleng = len(strngword) # Check if the length of the word is even or not # using the If conditional Statement. if(wordleng % 2 == 0): # If it is true then print the word. print(strngword)
Output:
Enter some random string = hello this is btechgeeks online coding platform for btech students Even length words in the given string [ hello this is btechgeeks online coding platform for btech students ] are: this is btechgeeks online coding platform students
Related Programs:
- python program to print largest even and largest odd number in a list
- python program to print sum of negative numbers positive even numbers and positive odd numbers in a list
- python program to count the frequency of words appearing in a string using a dictionary
- python program to print all permutations of a string in lexicographic order without recursion
- python program to calculate the number of words and the number of characters present in a string
- python program to accept a hyphen separated sequence of words as input and print the words in a hyphen separated sequence after sorting them alphabetically
- python program to print nth word in a given string