Remove Vowels from String in Python

Remove Vowels from String in Python | Previously we developed the Python program to check character is vowels or consonants, check if the string start with a vowel, count the vowel in the string, and print the vowel in the string. Now in this post, we will remove vowels from a string in python.

Python Program to Remove all Vowels from a given String

In the below code, we use the for loop to remove all vowels from a given string. Give the user-defined function to remove vowels from a string.

# 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)

string = input('String: ')
removeVowels(string)

Output for the different input values:-

String: Know Program
Knw Prgrm

String: Python Program
Pythn Prgrm

String: vowel and consonant
vwl nd cnsnnt

Python Code to Remove Vowels from a String

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.

# 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))

string = input('String: ')
print(removeVowels(string))

Output:-

String: remove vowels from a string
rmv vwls frm strng

Remove Vowels from String in Python 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)

string = input('String: ')
removeVowels(string)

Output:-

Enter any string: java
jv

Using String Translate Methods

The translate method can be replaced or translated 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)

string = input('String: ')
removeVowels(string)

Output:-

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 do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.