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
test_str = "Gfg is best for geeks"
print ( "The original string is : " + test_str)
res = []
for chr in test_str:
if chr in "aeiouAEIOU" :
res.extend( chr )
res = "".join(res)
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
test_str = "Gfg is best for geeks"
print ( "The original string is : " + test_str)
res = " ".join([chr for chr in test_str if chr in " aeiouAEIOU"])
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
test_str = "Gfg is best for geeks"
print ( "The original string is : " + test_str)
vow = "aeiouAEIOU"
res = ""
for i in test_str:
if i not in vow:
test_str = test_str.replace(i, "")
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
import operator as op
test_str = "Gfg is best for geeks"
print ( "The original string is : " + test_str)
vowels = "aeiouAEIOU"
res = []
for chr in test_str:
if op.countOf(vowels, chr ) > 0 :
res.extend( chr )
res = "".join(res)
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
test_str = "Gfg is best for geeks"
print ( "The original string is : " + test_str)
res = re.sub(r '[^aeiouAEIOU]+' , '', test_str)
print ( "String after sentants removal : " + str (res))
|
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
test_str = "Gfg is best for geeks"
print ( "The original string is : " + test_str)
res = ""
vow = [ 97 , 101 , 105 , 111 , 117 ]
for chr in test_str:
if ord ( chr ) in vow:
res + = chr
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:
- Initialize the string to be processed.
- 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.
- Use the filter() function with the lambda function and the string to create a filter object that contains only the vowels.
- Convert the filter object to a string using the join() method to get the final output string.
Python3
test_str = "Gfg is best for geeks"
print ( "The original string is : " + test_str)
res = ' '.join(filter(lambda x: x in ' aeiouAEIOU', test_str))
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:
- Initialize the string to be processed.
- Use numpy to convert the string to a numpy array of integers, using the ‘fromstring’ method.
- Define a boolean mask to identify vowels, using the numpy ‘isin’ method.
- Use the boolean mask to filter the array to retain only vowels.
- Use numpy ‘chararray’ method to convert the numpy array back to a string.
Python3
import numpy as np
test_str = "Gfg is best for geeks"
print ( "The original string is : " + test_str)
arr = np.array([ ord (c) for c in test_str])
vowels = np.array([ 97 , 101 , 105 , 111 , 117 ])
res_arr = arr[np.isin(arr, vowels)]
res = "".join([ chr (c) for c in res_arr])
print ( "String after consonants removal : " + str (res))
|
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).
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 May, 2023
Like Article
Save Article