How to remove vowels from string in Python?

Remove Vowels From String

A string is a data type that can consist of any combination of letters, numbers, and symbols. A string can contain both vowels and consonants of alphabets borrowed from the language of English. In this article, we are going to remove vowels from a string using Python. There are different techniques which can be deployed to get this done in Python viz.

  • The Loop Method
  • Using the Regular Expression
  • The Iterator & Join Technique

Each listed method works on an exclusive logic to define the input string and then remove the vowels associated with it. In simpler terms, these techniques make use of the loops or formulations to check and remove the listed vowels from the given string and then return the output string without any vowels.


Method I – The Loop Method

This method would loop around in search of the vowels in the given input string. The notable point here is that we include both uppercase & lowercase vowels while declaring what to detect in the string. This is done to factor in the effect of difference cases in which the letters are available in the string.

We shall get started by assigning hippopotamus as an input string, followed by listing down all the vowels to be identified and removed from the string. Also we shall define a variable result to be empty.

string = "hippopotamus"
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
result = ""
Assigning Input String And Vowels
Assigning Input String and Vowels

Now we shall create an iteration using a for loop combined with an if statement to extract the result as string without vowels. Have a look at its construction below.

for i in range(len(string)):
    if string[i] not in vowels:
        result = result + string[i]
Constructing Vowel Removal Code
Constructing the Vowel Removal Code

Once done, the print function shall be used to return the result.

print("\n After removing Vowels:", result)
Print Function To Return The Result
Displaying the Result

When the code is run, the following result is returned.

Resultant String Without Vowels
Resultant String Without Vowels

Method II – Using the Regular Expressions

An alternate technique that can be deployed to remove the vowels from the input string is using the ‘re’ – regular expression. We will get started by importing it from Python’s standard library, followed by defining a customized module. Within this module we shall provide the condition that states how to remove the vowels from the input string.

import re
def rem_vowel(string):
    return(re.sub("[aeiouAEIOU]","",string))
Creating A Custom Function
Creating a Custom Function to Remove Vowels

Declare an input string & return the output using the print statement as shown below.

string = "Hippopotamus"
x = rem_vowel(string)
print(x)
Result Removing Vowels
Result Removing Vowels

Method III – The Iterator & Join Technique

This is a two step method in which the extraction of the consonants from the string is isolated at the first step followed by the joining of consonants together to form a word. Let’s get started by defining the string & vowels as shown below.

Declaring Vowels Input String
Declaring Vowels Input String

This is followed by constructing a variable ‘result’ with a set of instructions to remove the vowels and return only the consonants from the input string. But, it is to be noted that the returned value will not be in the form of a word, rather be in the form of separated letters as shown below.

result = [letter for letter in string if letter.lower() not in vowels]
print(result)
Isolated Consonants Returned
Isolated Consonants Returned

Now it’s time to join the isolated letters using join command as shown below.

final = ''.join(result)
print(final)
Combining Consonants Using The Join Function
Combining Consonants Using The Join Function

Conclusion

We’ve explored three different techniques to remove vowels from a given input string using Python. Each method offers a unique approach, from using loops and conditionals to employing regular expressions or the iterator and join techniques. Having a variety of methods available will allow you to choose the most suitable approach, depending on your specific text processing needs. Continue learning and experimenting with Python to level up your skills and expand your toolkit for tackling various challenges. You may also be interested in learning how to convert Hexadecimal to RGB values in Python.


Reference: