Python program to print even length words in a string
Last Updated :
04 Mar, 2025
The task of printing even-length words from a string in Python involves identifying and extracting words whose lengths are divisible by 2. Given an input string, the goal is to filter out words with an even number of characters and display them. For example , s = "Python is great", the even-length words are ["Python", "is"] with lengths [6, 2]. "Python" (6) and "is" (2) are included as their lengths are even, while "great" (5) is excluded as it is odd.
Using list comprehension
This method splits the string into words, filters out even-length words using list comprehension and joins them into a single output string. It is the fastest and cleanest approach, as it avoids multiple print calls and unnecessary loops.
Python
s = "This is a python language"
# split the sentence into words
wrds = s.split()
# filter words with even length
even_wrds = [w for w in wrds if len(w) % 2 == 0]
# Join the filtered words back into a sentence
res = " ".join(even_wrds)
print(res)
OutputThis is python language
Explanation: s is split into words using .split(), creating a list. List comprehension then filters words with even lengths (len(w) % 2 == 0) into even_wrds. Finally, join() combines these words into a space-separated string .
Using filter()
This approach uses filter() to extract even-length words and lambda to define the condition, making it a functional programming alternative. The join() function combines the words into a single output.
Python
s = "Python is great"
# split the sentence into words
wrds = s.split()
# filter words with even length
even_wrds = filter(lambda w: len(w) % 2 == 0, wrds)
# join the words back into a string
res = " ".join(even_wrds)
print(res)
Explanation: filter() with a lambda (len(w) % 2 == 0) extracts even-length words, which are then joined into a space-separated string using join().
Using generator expression with map
A memory-efficient approach, this method generates words dynamically instead of storing them in a list. It filters even-length words on the fly using a generator expression and then joins them efficiently.
Python
a = "geeks for geek"
# split the sentence into words
wrds = a.split()
# filter words with even length
even_wrds = (w for w in wrds if len(w) % 2 == 0)
# convert words to strings
even_wrd_str = map(str, even_wrds)
# join the words back into a sentence
res = " ".join(even_wrd_str)
print(res)
Explanation: generator expression filters even-length words dynamically, map(str, even_wrds) ensures they are strings and join() combines them into a space-separated string.
Using for loop
A simple, beginner-friendly approach is using a for loop. This method iterates through each word, checks its length and prints it if it's even. While not the most optimized, it is easy to understand and implement.
Python
a = "My name is shakshi"
# split the sentence into words
wrds = a.split()
# loop through each word and check if its length is even
for w in wrds:
if len(w) % 2 == 0:
print(w, end=" ")
Explanation: for loop iterates through each word, checking if its length is even (len(w) % 2 == 0). If true, the word is printed with a space separator using end=" ".
Similar Reads
Reverse Words in a Given String in Python In this article, we explore various ways to reverse the words in a string using Python. From simple built-in methods to advanced techniques like recursion and stacks. We are going to see various techniques to reverse a string.Using split() and join()Using split() and join() is the most common method
2 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Add padding to a string in Python Padding a string involves adding extra characters such as spaces or specific symbols to adjust its length or align it in a visually appealing way. Letâs dive into the most efficient methods to add padding to a string in Python.Using Python f-stringsF-strings allow us to specify padding directly with
2 min read
String Alignment in Python f-string String alignment in Python helps make text look neat and organized, especially when printing data of different lengths. Without formatting, output can appear messy and hard to read. Pythonâs f-strings make it easy to align text by controlling its position within a set space. Pythonâs f-strings allow
2 min read
How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str
4 min read