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<char> 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 << remVowel(str) << endl;

        return 0;

    }

    Java

    import java.util.Scanner;

    public class Practice {

        public static void main(String[] args)

        {

            Scanner sc = new Scanner(System.in);

            String s = sc.nextLine();

            for (int i = 0; i < s.length(); i++) {

                if (s.charAt(i) == 'a' || s.charAt(i) == 'e'

                    || s.charAt(i) == 'i' || s.charAt(i) == 'o'

                    || s.charAt(i) == 'u' || s.charAt(i) == 'A'

                    || s.charAt(i) == 'E' || s.charAt(i) == 'I'

                    || s.charAt(i) == 'O'

                    || s.charAt(i) == 'U') {

                    continue;

                }

                else {

                    System.out.print(s.charAt(i));

                }

            }

        }

    }

    Python3

    def rem_vowel(string):

        vowels = ['a','e','i','o','u']

        result = [letter for letter in string if letter.lower() not in vowels]

        result = ''.join(result)

        print(result)

    string = "GeeksforGeeks - A Computer Science Portal for Geeks"

    rem_vowel(string)

    string = "Loving Python LOL"

    rem_vowel(string)

    C#

    using System;

    using System.Text;

    using System.Linq;

    using System.Collections.Generic;

    public class Test

    {   

        static String remVowel(String str)

        {

             char []vowels = {'a', 'e', 'i', 'o', 'u','A','E','I','O','U'};

             List<char> al = vowels.OfType<char>().ToList();;

             StringBuilder sb = new StringBuilder(str);

             for (int i = 0; i < sb.Length; i++) {

                 if(al.Contains(sb[i])){

                    sb.Replace(sb[i].ToString(), "") ;

                    i--;

                 }

            }

            return sb.ToString();

        }

        public static void Main()

        {

            String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";

            Console.Write(remVowel(str));

        }

    }

    Javascript

    Output

    GksfrGks -  Cmptr Scnc Prtl fr Gks

    Time Complexity: O(n), where n is the length of the string
    Space Complexity: O(1)

    We can improve the above solution by using Regular Expressions. 

    Implementation:

    C++

    #include

    using namespace std;

    string remVowel(string str)

    {

        regex r("[aeiouAEIOU]");

        return regex_replace(str, r, "");

    }

    int main()

    {

        string str = "GeeeksforGeeks - A Computer Science Portal for Geeks";    

        cout << (remVowel(str));

        return 0;

    }

    Java

    import java.util.Arrays;

    import java.util.List;

    class GFG

    {   

        static String remVowel(String str)

        {

             return str.replaceAll("[aeiouAEIOU]", "");

        }

        public static void main(String[] args)

        {

            String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";       

            System.out.println(remVowel(str));

        }

    }

    Python3

    import re

    def rem_vowel(string):

        return (re.sub("[aeiouAEIOU]","",string))           

    string = "GeeksforGeeks - A Computer Science Portal for Geeks"

    print(rem_vowel(string))

    C#

    using System;

    using System.Text.RegularExpressions;

    class GFG

    {

        static String remVowel(String str)

        {

            str = Regex.Replace(str, "[aeiouAEIOU]", "");

            return str;

        }

        public static void Main()

        {

            String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";    

            Console.WriteLine(remVowel(str));

        }

    }

    Output

    GksfrGks -  Cmptr Scnc Prtl fr Gks

    Time Complexity: O(n), where n is the length of the string
    Space Complexity: O(1)

    This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. 


    How do you remove a vowel?

    The algorithm is as follows;.
    Algorithm. START Step-1: Input the string Step-3: Check vowel presence, if found return TRUE Step-4: Copy it to another array Step-5: Increment the counter Step-6: Print END. ... .
    Example. ... .
    Output..

    How do you remove a letter from a string in Python?

    Using translate(): translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .

    How do you eliminate a letter from a string?

    In Python you can use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result. It is important to remember that the original string will not be altered because strings are immutable.

    How do you remove consecutive vowels in Python?

    Approach: Iterate string using a loop and check for the repetitiveness of vowels in a given sentence and in case if consecutive vowels are found then delete the vowel till coming next consonant and printing the updated string.