Q:

Python | Print EVEN length words

belongs to collection: Python String Programs

0

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

Logic:

  • 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Count vowels in a string... >>
<< Python | Program to print words with their length ...