Open In App
Related Articles

Python – Remove all consonants from string

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Sometimes, while working with Python, we can have a problem in which we wish to remove all the non vowels from strings. This is quite popular question and solution to it is useful in competitive programming and day-day programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop 
This is one of the ways in which this task can be performed. In this, we iterate through the list and then check for non presence of vowels and filter.
 

Python3

# Python3 code to demonstrate working of
# Remove all consonants from string
# Using loop
 
# initializing string
test_str = "Gfg is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Remove all consonants from string
# Using loop
res = []
for chr in test_str:
    if chr in "aeiouAEIOU":
        res.extend(chr)
res = "".join(res)
 
# printing result
print("String after consonants removal : " + str(res))

                    

Output
The original string is : Gfg is best for geeks
String after consonants removal : ieoee

 
Method #2 : Using list comprehension 
This is one of the ways in which this task can be performed. In this, we iterate through the list and then filter out vowels in similar manner but in one-liner.
 

Python3

# Python3 code to demonstrate working of
# Remove all consonants from string
# Using list comprehension
 
# initializing string
test_str = "Gfg is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Remove all consonants from string
# Using list comprehension
res = "".join([chr for chr in test_str if chr in "aeiouAEIOU"])
 
# printing result
print("String after consonants removal : " + str(res))

                    

Output
The original string is : Gfg is best for geeks
String after consonants removal : ieoee

The Time and Space complexity for all methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)

Method #3 : Using replace() method
There is one more way, while iterating through the string if consonant occur the we will replace that char with empty char.

Python3

# Python3 code to demonstrate working of
# Remove all consonants from string
# Using loop
 
# initializing string
test_str = "Gfg is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Remove all consonants from string
vow = "aeiouAEIOU"
res = ""
 
# Using loop
for i in test_str:
    if i not in vow:
        test_str = test_str.replace(i, "")
 
# printing result
print("String after consonants removal : " + str(test_str))

                    

Output
The original string is : Gfg is best for geeks
String after consonants removal : ieoee

Method #4 : Using operator.countOf() method

Python3

# Python3 code to demonstrate working of
# Remove all consonants from string
# Using loop
import operator as op
# initializing string
test_str = "Gfg is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
vowels = "aeiouAEIOU"
# Remove all consonants from string
# Using loop
res = []
for chr in test_str:
    if op.countOf(vowels, chr) > 0:
        res.extend(chr)
res = "".join(res)
 
# printing result
print("String after consonants removal : " + str(res))

                    

Output
The original string is : Gfg is best for geeks
String after consonants removal : ieoee

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5 : Using re module (regular expressions)

Python3

import re
 
#initializing string
test_str = "Gfg is best for geeks"
 
#printing original string
print("The original string is : " + test_str)
 
#Remove all consonants from string
#Using regular expressions
res = re.sub(r'[^aeiouAEIOU]+', '', test_str)
 
#printing result
print("String after sentants removal : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

                    

Output
The original string is : Gfg is best for geeks
String after sentants removal : ieoee

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #6 : Using ord()+for loop

Python3

# Python3 code to demonstrate working of
# Remove all consonants from string
# Using loop
 
# initializing string
test_str = "Gfg is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Remove all consonants from string
# Using loop
res = ""
vow=[97, 101, 105, 111, 117]
for chr in test_str:
    if ord(chr) in vow:
        res+=chr
# printing result
print("String after consonants removal : " + str(res))

                    

Output
The original string is : Gfg is best for geeks
String after consonants removal : ieoee

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #7: Using filter() and lambda function

In this method, we can use the built-in filter() function with a lambda function to filter out all the consonants in the string.

Step-by-step approach:

  1. Initialize the string to be processed.
  2. Define a lambda function that will filter out all the consonants from the string. The lambda function should take one argument (a character), and it should return True if the character is a vowel and False otherwise.
  3. Use the filter() function with the lambda function and the string to create a filter object that contains only the vowels.
  4. Convert the filter object to a string using the join() method to get the final output string.

Python3

# Python3 code to demonstrate working of
# Remove all consonants from string
# Using filter() and lambda function
 
# initializing string
test_str = "Gfg is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Remove all consonants from string
# Using filter() and lambda function
res = ''.join(filter(lambda x: x in 'aeiouAEIOU', test_str))
 
# printing result
print("String after consonants removal : " + str(res))

                    

Output
The original string is : Gfg is best for geeks
String after consonants removal : ieoee

Time complexity: O(n)
Auxiliary space: O(n)

Method #8: Using numpy:

Algorithm:

  1. Initialize the string to be processed.
  2. Use numpy to convert the string to a numpy array of integers, using the ‘fromstring’ method.
  3. Define a boolean mask to identify vowels, using the numpy ‘isin’ method.
  4. Use the boolean mask to filter the array to retain only vowels.
  5. Use numpy ‘chararray’ method to convert the numpy array back to a string.

Python3

import numpy as np
 
# initializing string
test_str = "Gfg is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# convert the string to an array of Unicode code points
arr = np.array([ord(c) for c in test_str])
 
# select only the vowels (Unicode code points)
vowels = np.array([97, 101, 105, 111, 117])
res_arr = arr[np.isin(arr, vowels)]
 
# convert the array of Unicode code points back to a string
res = "".join([chr(c) for c in res_arr])
 
# printing result
print("String after consonants removal : " + str(res))
#This code is contributed  by Pushpa.

                    
Output:
The original string is : Gfg is best for geeks
String after consonants removal : ieoee

Time Complexity:

The code involves creating two NumPy arrays, which takes O(n) time, where n is the length of the input string.
The operation of selecting only the vowels involves a comparison between two arrays, which takes O(n) time as well.
The final step of converting the array back to a string takes O(n) time too.
Therefore, the total time complexity of the code is O(n).
Space Complexity:

The code creates two NumPy arrays, which take O(n) space, where n is the length of the input string.
There are no other significant memory allocations in the code.
Therefore, the total space complexity of the code is also O(n).



Last Updated : 11 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads