Home »
Python »
Python Programs
Python | Print EVEN length words
Here, we are going to implement a Python program, in which we will declare and string and print only EVEN length words.
Submitted by IncludeHelp, on August 01, 2018
Problem statement
Given a string, and we have to print the EVEN length words in Python.
Example
Input:
str: Python is a programming language
Output:
EVEN length words:
Python
is
language
Algorithm/Logic
The following are the steps to print the EVEN length words from a string in Python:
- To print the EVEN length words, we have to check length of each word.
- For that, first of all, we have to extract the words from the string and assigning them in a list.
- Iterate the list using loop.
- Count the length of each word, and check whether length is EVEN (divisible by 2) or not.
- If word's length is EVEN, print the word.
Python program to print even length words in a string
# print EVEN length words of a string
# declare, assign string
str = "Python is a programming language"
# extract words in list
words = list(str.split(" "))
# print string
print("str: ", str)
# print list converted string i.e. list of words
print("list converted string: ", words)
# iterate words, get length
# if length is EVEN print word
print("EVEN length words:")
for W in words:
if len(W) % 2 == 0:
print(W)
Output
str: Python is a programming language
list converted string: ['Python', 'is', 'a', 'programming', 'language']
EVEN length words:
Python
is
language
Python String Programs »