How do I convert all the vowels within a string to white-spaces in python

First of all instead of setting string = s simply loop through s as it is. Also, instead of looping through both s and vowels just loop s and check if the letter is in vowels, if so add a white space to string if not add the letter to string. Here is the code:

def w_space (s):
    vowels = "aeiouAEIOU"
    string = ""
    for a in s:
        if a in vowels:
            string += " "
        else:
            string += a
    return string

Leave a Comment