python打印所有偶数
Given a string, and we have to print the EVEN length words in Python.
给定一个字符串,我们必须在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.
计算每个单词的长度,并检查长度是否为偶数(可被2整除)。
If 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
翻译自: https://www.includehelp.com/python/print-even-length-words.aspx
python打印所有偶数



