Write a Python Function to Print Even Length Words in a String

Given a string containing a words  as a input to the Python program your task is to to iterate all the words present in the list and try to check if the word  length is an even number,

if the length of the word is even number then print the  word  as an output of the program,  you have to perform this using a  python function.

 

 

Write a Python Function to Print Even Length Words in a String

 


Write a Python Function to Print Even Length Words in a String

Let’s define a function named as print_even_words(),  and pass the input argument as a string type  to the function. Print a user friendly message that is  even length words  of a string are…

Let's use the following to iterate all the words present in the string by setting the string by the space. Using the statement check if the length of word is completely divisible by 2 if so print the word as even length word.

def print_even_words(words):
   print("\nEven Length Words in String is ...")
   for word in words.split(" "):
       if len(word) % 2 == 0:
           print(word, end=" ")




In the main program take a variable named as text to store the string and using input function call ask a user to enter the string.  

Once the string is entered by the user, call the Python function named print_even_words() by passing the input argument as string.

text = input("Enter string : ")
print_even_words(text)




Final program
=====================

def print_even_words(words):
   print("\nEven Length Words in String is ...")
   for word in words.split(" "):
       if len(word) % 2 == 0:
           print(word, end=" ")


text = input("Enter String : ")
print_even_words(text)




Output
==============

Enter String : how is your day going

Even Length Words in String is ...
is your



Output 2
=============

Enter String : write a python function to print even length words in a string

Even Length Words in String is ...
python function to even length in string 

 

 

 

OUTPUT 3
====================
Enter List Size : python even words program using function

Even Length Words in String is ...
python even function





Conclusion
==================

Execute the above program by passing the random string to it as an input argument and note down the result.

Comment down below if you have any suggestions to improve the above program.





Post a Comment

0 Comments