Introduction
In this blog post, we will learn how to write a Python program to print even length words in a string. This program will accept a string as input and print all the words in the string that have an even number of characters.
Why would we want to do this?
There are a few reasons why we might want to write a Python program to print even length words in a string. For example, we could use it to:
- Extract keywords from text
- Identify patterns in data
- Generate code
- Solve puzzles
How does the program work?
The program works by first splitting the input string into a list of words using the string.split()
method. Then, it iterates over the list of words and checks the length of each word using the len()
method. If the length of the word is even, then the word is printed to the console.
Python code
The following Python code prints all the even length words in a string:
def print_even_length_words(string):
"""Prints all even length words in a string.
Args:
string: A string.
"""
for word in string.split():
if len(word) % 2 == 0:
print(word)
# Example usage:
string = "This is a sample string."
print_even_length_words(string)
Output
This
is
sample
Explanation
The program works by first splitting the input string into a list of words using the string.split()
method. Then, it iterates over the list of words and checks the length of each word using the len()
method. If the length of the word is even, then the word is printed to the console.
Variations
The program can be modified to print even length words that start with a specific letter, or that contain a specific substring. For example, the following program prints all the even length words in a string that start with the letter "t":
def print_even_length_words_starting_with_t(string):
"""Prints all even length words in a string that start with the letter t.
Args:
string: A string.
"""
for word in string.split():
if len(word) % 2 == 0 and word[0] == "t":
print(word)
# Example usage:
string = "This is a sample string."
print_even_length_words_starting_with_t(string)
Output:
This
The program can also be modified to print even length words that contain a specific substring. For example, the following program prints all the even length words in a string that contain the substring "sample":
def print_even_length_words_containing_sample(string):
"""Prints all even length words in a string that contain the substring sample.
Args:
string: A string.
"""
for word in string.split():
if len(word) % 2 == 0 and "sample" in word:
print(word)
# Example usage:
string = "This is a sample string."
print_even_ length_words_containing_sample(string)
Output:
sample
Using regular expressions
We can also use regular expressions to print even length words in a string. For example, the following program prints all the even length words in a string that contain at least one vowel:
import re
def print_even_length_words_containing_vowels(string):
"""Prints all even length words in a string that contain at least one vowel.
Args:
string: A string.
"""
vowels = re.compile(r"[aeiou]")
for word in string.split():
if len(word) % 2 == 0 and vowels.search(word):
print(word)
# Example usage:
string = "This is a sample string."
print_even_length_words_containing_vowels(string)
Output:
is
sample
Conclusion
In this blog post, we learned how to write a Python program to print even length words in a string. We also saw how to modify the program to print even length words that start with a specific letter, or that contain a specific sub string. Additionally, we saw how to use regular expressions to print even length words in a string that meet certain criteria.
These programs can be used for a variety of tasks, such as:
- Extracting keywords from text
- Identifying patterns in data
- Generating code
- Solving puzzles
For example, we could use the program to extract all the even length words from a book and then use those words to generate a new poem. Or, we could use the program to identify all the even length words in a dataset of customer reviews and then use those words to identify common themes.
The possibilities are endless!
Additional thoughts
Here are some additional thoughts on how to use the program:
- We could use the program to create a word cloud of even length words from a text.
- We could use the program to filter a list of words to only include even length words.
- We could use the program to validate user input, such as ensuring that a password is at least six characters long.
- We could use the program to develop a new game or puzzle.
I encourage you to experiment with the program and see how you can use it to solve your own problems.
0 Comments