Need to write a function that returns all of the vowels in a word

Question:

I am new to learning python and am struggling to create a Function to return a string only of vowels from word no matter if its a blank string, string with no vowels, or string with all vowels.

This is what I wrote:

def vowels_only(word):
    word = "banana"
    vowels = 'aeiouy'
    for letters in word:
        if letters == vowels:
            return letters

I expected to get: "aaa"

actual: None

What did I do wrong?

Asked By: Miles Russell

||

Answers:

You should consider using lists for both your word and the vowels you want to find. It will be much easier to iterate over a list and then you can make a blank list to append all the vowels next time. Read the documentation about lists and try to post your proper code next time to get better help.

Answered By: goatishnewcastle49

You’ll have to check each letter against the values in vowels.

def vowels_only(word):
    vowels = list('aeiouy')
    letters = []
    for letter in word:
        if letter in vowels and letter not in letters:
            letters.append(letter)
    return letters
Answered By: Reilas

Glad to know that you have taken your first step in learning Python.

There are few mistakes in your code which it might run improperly.

  1. word should not be inside vowels_only() function as the function needs to take word as function parameter.

  2. return should not be used as function will stop running after returning a value.

Two methods can be used to solve this problem.

def vowels_only(word):
    vowels = 'aeiouy'
    filtered = []
    for letters in word:
        if letters in vowels:
            filtered.append(letters)
    return "".join(filtered)

print(vowels_only("banana"))

In the above code, "banana" is taken as parameter and filtered is a list I declared to filter the vowels found from the word in Line 5.

def vowels_only(word):
    vowels = 'aeiouy'
    for letters in word:
        if letters in vowels:
            yield letters

print(*vowels_only("banana"))

The second method, using yield. Return will stop the function from runnning. Use yield which is an alternative to it. However, yield will return generator object, hence pointer with * symbol is used to locate the object.

Answered By: WenHao1223
a = "banana"
b = "".join([t for t in a if t in "aeiouy"])
Answered By: Xiaomin Wu

str is already an Iterable type. There is no reason to convert it to a list or create any temporary Iterable types for processing.

Your goal is:

  1. iterate over each character in a word (for c in word)
  2. store characters that meet a specific condition. (c in 'aeiouy')

You can work directly with the word using a generator. join is used to convert the generator result to a string.

def vowels_only(word):
    return "".join(c for c in word if c in 'aeiouy')

An alternate method to use is filter. Each character of the word will be passed to the lambda. Every character that passes the lambda condition will be kept. join is used to convert the filter result to a string.

def vowels_only(word):
    return "".join(filter(lambda c: c in 'aeiouy', word))

A 3rd option is to use a regular expression, and replace the unwanted characters with an empty character. This method is over-engineered for your needs, but it’s another example of the word being processed directly.

import re; from functools import partial

vowels_only = partial(re.compile(r'[^aeiouy]').sub, '')
Answered By: OneMadGypsy
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.