Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print even length words in a string
In Python, you can find and print all words in a string that have even length (divisible by 2). This is useful for text processing tasks where you need to filter words based on their character count.
For example, in the string "A big cube was found inside the box", the words "cube" (length 4) and "inside" (length 6) have even lengths.
Basic Approach
The simplest method involves splitting the string into words and checking each word's length ?
def print_even_length_words(text):
# Split the string into individual words
words = text.split()
for word in words:
# Check if word length is even
if len(word) % 2 == 0:
print(word)
# Example usage
sample = "this is a test string"
print_even_length_words(sample)
this is test string
Using filter() with Lambda
Python's filter() function provides a more functional approach ?
def print_even_length_words(text):
words = text.split()
even_words = filter(lambda x: len(x) % 2 == 0, words)
for word in even_words:
print(word)
# Example usage
print_even_length_words("this is a test string")
this is test string
Using List Comprehension
List comprehension offers a concise Python-style solution ?
def print_even_length_words(text):
even_words = [word for word in text.split() if len(word) % 2 == 0]
for word in even_words:
print(word)
# Example usage
print_even_length_words("this is a test string")
this is test string
One-liner Approach
You can combine unpacking with print() for a single line solution ?
def print_even_length_words(text):
print(*[word for word in text.split() if len(word) % 2 == 0], sep='\n')
# Example usage
print_even_length_words("this is a test string")
this is test string
Handling Punctuation
When dealing with sentences containing punctuation, you may need to clean the text first ?
def print_even_length_words(text):
# Remove common punctuation
cleaned_text = text.replace('.', '').replace(',', '').replace('!', '').replace('?', '')
words = cleaned_text.split()
for word in words:
if len(word) % 2 == 0:
print(word)
# Example with punctuation
sample = "Hello world! This is a test sentence."
print_even_length_words(sample)
world This is test
Returning Results as List
Sometimes you might want to return the results instead of printing them ?
def get_even_length_words(text):
return [word for word in text.split() if len(word) % 2 == 0]
# Example usage
text = "Python programming is fun and educational"
even_words = get_even_length_words(text)
print("Even length words:", even_words)
print("Count:", len(even_words))
Even length words: ['programming', 'is', 'fun', 'and'] Count: 4
Comparison of Methods
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Basic Loop | High | Good | Beginners, debugging |
| filter() | Medium | Good | Functional programming |
| List Comprehension | High | Best | Pythonic code |
| One-liner | Low | Good | Code golf |
Conclusion
Use list comprehension for the most Pythonic approach to filter even-length words. For simple cases, a basic loop is perfectly readable and efficient. Remember to handle punctuation when processing real-world text data.
