Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to print even length words in a string

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 31 Jan, 2023
Improve Article
Save Article

Given a string. The task is to print all words with even length in the given string.

Examples: 

Input: s = "This is a python language"
Output: This is python language

Input: s = "i am laxmi"
Output: am

Method: Finding even length words using for loop and if statement and without using the def function. First split the given string using the split() function and then iterate the words of a string using for loop. Calculate the length of each word using the len() function. If the length is even, then print the word.

Python3




# Python code
# To print even length words in string
 
#input string
n="This is a python language"
#splitting the words in a given string
s=n.split(" ")
for i in s:
  #checking the length of words
  if len(i)%2==0:
    print(i)
 
# this code is contributed by gangarajula laxmi

Output

This
is
python
language

Time Complexity: O(n), where n is the length of the given string
Auxiliary Space: O(n)

Approach: Split the string using split() function. Iterate in the words of a string using for loop. Calculate the length of the word using len() function. If the length is even, then print the word. Below is the Python implementation of the above approach: 

Python3




# Python3 program to print
# even length words in a string
 
def printWords(s):
     
    # split the string
    s = s.split(' ')
     
    # iterate in words of string
    for word in s:
         
        # if length is even
        if len(word)%2==0:
            print(word)
 
 
# Driver Code
s = "i am muskan"
printWords(s)

Output

am
muskan

Time Complexity: O(n), where n is the length of the given string
Auxiliary Space: O(n)

Method: Using the lambda function

Python3




# Python code
# To print even length words in string
 
#input string
n="geeks for geek"
#splitting the words in a given string
s=n.split(" ")
l=list(filter(lambda x: (len(x)%2==0),s))
print(l)

Output

['geek']

Time Complexity: O(n), where n is the length of the given string
Auxiliary Space: O(n)

Method: Using list comprehension

Python3




# python code to print even length words
 
n="geeks for geek"
s=n.split(" ")
print([x for x in s if len(x)%2==0])

Output

['geek']

Time Complexity: O(n), where n is the length of the given string
Auxiliary Space: O(n)

Method: Using the enumerate function 

Python3




# python code to print even length words
 
n="geeks for geek"
s=n.split(" ")
print([x for i,x in enumerate(s) if len(x)%2==0])

Output

['geek']

Time Complexity: O(n), where n is the length of the given string
Auxiliary Space: O(n)

Method : Using Recursion

Python3




#recursive function to print even length words
def PrintEvenLengthWord(itr,list1):
  if itr == len(list1):
    return
  if len(list1[itr])%2 == 0:
    print(list1[itr])
  PrintEvenLengthWord(itr+1,list1)
  return
str = "geeks for geek"
l=[i for i in str.split()]
PrintEvenLengthWord(0,l)

Output

geek

Time Complexity: O(n), where n is the length of the given string
Auxiliary Space: O(n)

Method :  Using the filter function and lambda function:

 Use the filter function and lambda function to find even length words
 

Python




s="geeks for geek"
 
# Split the string into a list of words
words = s.split(" ")
 
# Use the filter function and lambda function to find even length words
even_length_words = list(filter(lambda x: len(x) % 2 == 0, words))
 
# Print the list of even length words
print(even_length_words)
#This code is contributed by Edula Vinay Kumar Reddy

Output

['geek']

Method :  Using the itertools.filterfalse() function

Python3




import itertools
s="geeks for geek"
 
# Split the string into a list of words
words = s.split(" ")
 
# Use the itertools.filterfalse function and lambda function to find even length words
even_length_words = list(itertools.filterfalse(lambda x: len(x) % 2 != 0, words))
 
# Print the list of even length words
print(even_length_words)

Output

['geek']

This approach has a time complexity of O(n), where n is the length of the string, and an auxiliary space of O(n).


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!