如何使用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 + 1I'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_wordThe straightforward way:
for each letter in the text if it is a vowel, print it otherwise print a spaceTranslates 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 resultUsing the regular expression pattern language:
import re word = "james is funny" new_word = re.sub('[^aeiou]', ' ', 'james is funny') print new_word更多推荐
发布评论