Remove Vowels from String in Python

The alphabets ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (in uppercase) and ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels, and remaining alphabets are called consonants. Previously we developed the Python program to check a character is a vowel or consonant, check if a string starts with the vowel, count vowels in a string, and print vowels in a string. Now in this post, we will remove vowels from a string in python.

Python Program to Remove all Vowels from a given String

This python program using the for loop to remove all vowels from a given string. We will take a user-defined function to remove vowels from a string. Then, we will take a string while declaring the variables. Finally, call the function and the result will be displayed on the screen.

# Python program to remove all vowels from string

def removeVowels(string):
    vowel = 'aeiou'
    #find vowel in string
    for ch in string.lower():
        if ch in vowel:
            #remove vowels
            string = string.replace(ch, '')

    #print string without vowels
    print(string)
  
# take input
string = input('Enter any string: ')

# calling function
removeVowels(string)

Output for the different input values:-

Enter any string: Know Program
Knw Prgrm

Enter any string: Python Program
Pythn Prgrm

Enter any string: vowel and consonant
vwl nd cnsnnt

Python Code to Remove Vowels from a String

This python program also performs the same task but in a different way. In this method, we have to use Regular Expressions. The re module to work with regular expressions. The sub-method is used to replace substrings in a string using a regular expression. We will use this method to remove vowels from a given string with the help of regex. This is a better and efficient way to remove vowels from string.

# Python program to remove all vowels from string

import re  #importing regular expression

# function for remove vowels from string
def removeVowels(string):
    return (re.sub('[aeiouAEIOU]','',string))
  
# take input
string = input('Enter any string: ')

# calling function and print string without vowels
print(removeVowels(string))

Output:-

Enter any string: remove vowels from a string
rmv vwls frm strng

Using join Methods

We will write a program in a more simple way using join methods. We will put all the characters of the string that are consonant(not vowel) in an array and join them to a string.

# Python program to remove all vowels from string

# function for remove vowels from string
def removeVowels(string):
    remove_str = ''.join([x for x in string if x.lower() not in 'aeiou'])
    #print string without vowels
    print(remove_str)

# take input
string = input('Enter any string: ')

# calling function and print string without vowels
removeVowels(string)

Output:-

Enter any string: java
jv

Using String Translate Methods

The translate method can be replaced or translate into characters in a string using a mapping table.

# Python program to remove all vowels from string

# function for remove vowels from string
def removeVowels(string):
    vowels = 'AEIOUaeiou'
    #remove vowels
    translate = str.maketrans(dict.fromkeys(vowels))
    remove_str = string.translate(translate)
    #print string without vowels
    print(remove_str)

# take input
string = input('Enter any string: ')

# calling function
removeVowels(string)

Output:-

Enter any string: PROGRAMING Language
PRGRMNG Lngg

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *