栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python打印所有偶数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python打印所有偶数

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打印所有偶数

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号