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
Python string length The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method.Example:Pythons1 = "abcd" print(len(s1)) s2 = "" print(len(s2)) s3 = "a" print(len(s3))Output4 0 1 String len() Syntaxlen(string) ParameterString:
4 min read
Write your own len() in Python The method len() returns the number of elements in the list or length of the string depending upon the argument you are passing. How to implement without using len(): Approach: Take input and pass the string/list into a function which return its length.Initialize a count variable to 0, this count va
2 min read
How to Index and Slice Strings in Python? In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.Table of ContentIndexing Strings in PythonAccessing
2 min read
Pad or fill a string by a variable in Python using f-string In Python, we can pad or fill a string with a variable character to make it a specific length using f-strings. We can pad strings to the left, right, or centre by using f-strings with a variable as the padding character. Let's understand with the help of an example:Python s = 'GFG' pad_char = '-' wi
2 min read
Python String strip() Method strip() method in Python removes all leading and trailing whitespace by default. You can also specify a set of characters to remove from both ends. It returns a new string and does not modify the original.Let's take an example to remove whitespace from both ends of a string.Pythons = " GeeksforGeeks
2 min read