How do you remove a vowel from a string in python?

I know there are many correct solutions on this subject but I thought to add few fun ways of solving this problem. If you come from a C++/C# or Java, you will tend to use something like compare then action using the index to remove the unwanted entry in a for loop. Python has the Remove and Del functions. Remove function uses the value and del uses the index.The pythonic solution is in the last function. Lets see how we can do that:

Here we are using the index in a for loop and del function very similar in C++:

def remove_vol[str1]:
     #list2 = list1 # this won't work bc list1 is the same as list2 meaning same container#
    list1 = list[str1]
    list2 = list[str1]
    for i in range[len[list1]]:
        if list1[i] in volwes:
            vol = list1[i]
            x = list2.index[vol]
            del list2[x]
    print[list2]

Using the remove function:

def remove_vol[str1]: 
      list1 = list[str1]
      list2 = list[str1]
      for i in list1:
          if i in volwes:
              list2.remove[i]
      print[list2]

Building new string that does not contain the unwanted chars using their indexes:

def remove_vol[str1]:  
    list1 = list[str1]
    clean_str = ''
    for i in range[len[list1]]:
        if list1[i] not in volwes:
            clean_str += ''.join[list1[i]]
    print[clean_str]

Same as in the solution in above but using the value:

def remove_vol[str1]:
    list1 = list[str1]
    clean_str = ''
    for i in list1:
        if i not in volwes:
            clean_str += ''.join[i]
    print[clean_str]

How you should do it in python? Using list comprehension! It is beautiful:

def remove_vol[list1]:
    clean_str = ''.join[[x for x in list1 if x.lower[] not in volwes]]
    print[clean_str]

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a string, remove the vowels from the string and print the string without vowels. 

    Examples: 

    Input : welcome to geeksforgeeks
    Output : wlcm t gksfrgks
    
    Input : what is your name ?
    Output : wht s yr nm ?

    A loop is designed that goes through a list composed of the characters of that string, removes the vowels and then joins them.  

    Implementation:

    C++14

    #include

    using namespace std;

    string remVowel[string str]

    {

        vector vowels = {'a', 'e', 'i', 'o', 'u',

                               'A', 'E', 'I', 'O', 'U'};

        for [int i = 0; i < str.length[]; i++]

        {

            if [find[vowels.begin[], vowels.end[],

                          str[i]] != vowels.end[]]

            {

                str = str.replace[i, 1, ""];

                i -= 1;

            }

        }

        return str;

    }

    int main[]

    {

        string str = "GeeeksforGeeks - A Computer"

                     " Science Portal for Geeks";

        cout

    Chủ Đề