Python Program to Print Even-Length Words in a String. When working with text data in Python, it is common to encounter tasks that require specific conditions or filters. One such task is filtering and printing words of even length from a given string.
Table of Contents
Understanding the Problem
Our task is to create a Python program that takes a string as input and prints out the word that has an even number of characters. The program needs to handle various types of input, ensuring that it correctly identifies and outputs only those words whose length is even.
Python Program for Printing Even Words in a String
To complete the task, the Python program will follow the steps-
- Input a string from the user.
- Split the string into words.
- Check the length of each word.
- Print the word with an even length.
Python Program
def even_length_word(input_string):
words = input_string.split()
for word in words:
if len(word)%2 == 0:
print(word)
input_string = input("Enter a string ")
even_length_word(input_string)
The Python program to print even the length words of a string is a straightforward example of using string methods and conditional logic to filter and display data based on specific criteria.
Happy Coding & Learning
1 thought on “Python Program to Print Even-Length Words in a String”