Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print words with only 1 vowel?

my code so far, but since i'm so lost it doesn't do anything close to what I want it to do:

vowels = 'a','e','i','o','u','y'
#Consider 'y' as a vowel

input = input("Enter a sentence: ")

words = input.split()
if vowels == words[0]:
    print(words)

so for an input like this:

"this is a really weird test"

I want it to only print:

this, is, a, test

because they only contains 1 vowel.

like image 282
ajkey94 Avatar asked Jan 18 '26 07:01

ajkey94


2 Answers

Try this:

vowels = set(('a','e','i','o','u','y'))

def count_vowels(word):
    return sum(letter in vowels for letter in word)

my_string = "this is a really weird test"

def get_words(my_string):
    for word in my_string.split():
        if count_vowels(word) == 1:
            print word

Result:

>>> get_words(my_string)
this
is
a
test
like image 142
Akavall Avatar answered Jan 19 '26 22:01

Akavall


Here's another option:

import re

words = 'This sentence contains a bunch of cool words'

for word in words.split():
    if len(re.findall('[aeiouy]', word)) == 1:
        print word

Output:

This
a
bunch
of
words
like image 42
Kenosis Avatar answered Jan 19 '26 22:01

Kenosis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!