Python Print EVEN length words

Here, we are going to actualize a Python program, in which we will pronounce and string and print just EVEN length words.

Given a string, and we need to print the EVEN length words in Python.

Example:

    Input:
    str: Python is a programming language

    Output: 
    EVEN length words:
    Python 
    is
    language

Rationale:

  • To print the EVEN length words, we need to check the length of each word.
  • For that, as a matter of first importance, we need to extricate the words from the string and relegating them in a list.
  • Emphasize the list utilizing circle.
  • Tally the length of each word, and check whether the length is EVEN (distinguishable by 2) or not.
  • On the off chance that word’s length is EVEN, print the word.

Program:

# 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

Leave a Comment

error: Alert: Content is protected!!