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.
import re
word = "james is funny"
print re.sub('[^aeiou]','',word)
OUTPUT:
'aeiu'
Print only the vowel letters in the words:
print ''.join(c for c in text if c in 'aeiou')
Print only words containing vowels:
words = text.split()
def containsVowel(word):
any(c in word for c in 'aeiou')
words_with_vowels = [w for w in words if containsVowels(w)]
print words_with_vowels
The straightforward way:
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:
Using the regular expression pattern language:
import re
word = "james is funny"
new_word = re.sub('[^aeiou]', ' ', 'james is funny')
print new_word
vowels = "aeiou"
word = "james is funny and really fun!"
for v1 in range(len(vowels)):
for v2 in range (len(word)):
if vowels[v1] == word[v2]:
print("location in string", v2)
print("letter:", vowels[v1])
Python has a built in function which allows you to use strings as list and by using this you are able to loop through each letter of the string and compare with the vowels.
Eg
vowels[0] = a
word[10] = u
We need to find out the vowel from the word. Here passing the word
to re.findall()
, it will find all the vowels in the word and return as list of characters. We are joining the list to display as word.
import re
word = "james is funny"
k = re.findall(r'[aeiou]', word)
print ''.join(k)
Out: 'aeiu'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.