如何使用Python只显示带元音的单词中的字母(How to use Python to show only the letters from words with vowels)

编程入门 行业动态 更新时间:2024-10-25 20:29:06
如何使用Python只显示带元音的单词中的字母(How to use Python to show only the letters from words with vowels) VOWELS = "aeiou" word = "matt" word = "james is funny" cnt = 0 for v1 in VOWELS: print ("vowel", cnt) print("letter:", v1) cnt = cnt + 1 for v1 in word: print ("location in string", cnt) print("letter:", v1) cnt = cnt + 1

我一直试图弄清楚这几个小时,这让我发疯。 我只需要python来打印单词中的元音字母。

VOWELS = "aeiou" word = "matt" word = "james is funny" cnt = 0 for v1 in VOWELS: print ("vowel", cnt) print("letter:", v1) cnt = cnt + 1 for v1 in word: print ("location in string", cnt) print("letter:", v1) cnt = cnt + 1

I've been trying to figure this out for hours and it's driving me crazy. I just need python to print only the vowel letters in the words.

最满意答案

直截了当的方式:

对于文本中的每个字母 如果它是一个元音,打印它 否则打印一个空格

直接转换为Python(以及在打印时将所有内容保持在同一行上的调整):

VOWELS = "aeiou" word = "james is funny" for letter in word: if letter in VOWELS: print(letter, end='') else: print(' ', end='')

或者稍微更花哨的方式:

用空格替换所有非元音。 打印结果

使用正则表达式模式语言:

import re word = "james is funny" new_word = re.sub('[^aeiou]', ' ', 'james is funny') print new_word

The straightforward way:

for each letter in the text if it is a vowel, print it otherwise print a space

Translates directly into Python (along with a tweak to keep everything on the same line when printing):

VOWELS = "aeiou" word = "james is funny" for letter in word: if letter in VOWELS: print(letter, end='') else: print(' ', end='')

Or the slightly more fancy way:

Replace all non-vowels with spaces. Print the result

Using the regular expression pattern language:

import re word = "james is funny" new_word = re.sub('[^aeiou]', ' ', 'james is funny') print new_word

更多推荐

本文发布于:2023-07-15 22:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元音   只显示   如何使用   字母   词中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!