Python – Replace vowels in a string with a specific character K
Last Updated :
16 Mar, 2023
Given a string, replace all the vowels with character K.
Input : test_str = “Geeks for Geeks”; K=’#’
Output : “G##ks f#r G##ks”
Explanation : All the vowels in test_str are replaced by a given particular character.
Input : test_list = “GFG”; K=”$”
Output : GFG
Explanation : As test_str contained no vowel, so the same string is returned.
Method #1 : Using loop + replace()
Initially create a string of vowels and then iterate through the given test_str, on detecting a vowel in test_str replace the vowel with K using replace() method.
Python3
def replaceVowelsWithK(test_str, K):
vowels = 'AEIOUaeiou'
for ele in vowels:
test_str = test_str.replace(ele, K)
return test_str
input_str = "Geeks for Geeks"
K = "#"
print ( "Given String:" , input_str)
print ( "Given Specified Character:" , K)
print ( "After replacing vowels with the specified character:" ,
replaceVowelsWithK(input_str, K))
|
Output
Given String: Geeks for Geeks
Given Specified Character: #
After replacing vowels with the specified character: G##ks f#r G##ks
Time Complexity: O(n), where n is the length of the input string. The for loop iterates over the vowels in the string only once.
Auxiliary Space: O(1), as we are not using any data structure to store the vowels or the output string, and are instead modifying the input string in place.
Method #2 : Using nested loop
Here, we first convert the given string into a list and then create a list of vowels and an empty list i.e new_string. After that elements of both string_list and vowel_list are compared and if the element is a vowel then K is appended to new_string else the element of the given string is appended.
Python3
def replaceVowelsWithK(test_str, K):
vowels_list = [ 'A' , 'E' , 'I' , 'O' , 'U' , 'a' , 'e' , 'i' , 'o' , 'u' ]
new_string = []
string_list = list (test_str)
for char in string_list:
for char2 in vowels_list:
if char = = char2:
new_string.append(K)
break
else :
new_string.append(char)
return (''.join(new_string))
input_str = "Geeks for Geeks"
K = "#"
print ( "Given String:" , input_str)
print ( "Given Specified Character:" , K)
print ( "After replacing vowels with the specified character:" ,
replaceVowelsWithK(input_str, K))
|
Output
Given String: Geeks for Geeks
Given Specified Character: #
After replacing vowels with the specified character: G##ks f#r G##ks
Time complexity: O(n^2), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.
Method#3: Using re.sub() method.
Here, we match the pattern with the help of regular expression and use re.sub method of regular expression module to substitute the matched pattern in the string with specified character.
Python3
import re
def replaceVowelsWithK(test_str, K):
vowels = 'AEIOUaeiou'
return re.sub(rf '[{vowels}]' , K, test_str)
input_str = "Geeks for Geeks"
K = "&"
print ( "Given String:" , input_str)
print ( "Given Specified Character:" , K)
print ( "After replacing vowels with the specified character:" ,
replaceVowelsWithK(input_str, K))
|
Output:
Given String: Geeks for Geeks
Given Specified Character: &
After replacing vowels with the specified character: G&&ks f&r G&&ks
Method#4: Using keys()+join()+append()+ vowels dictionary
Python3
vowels = {
"a" : 0 , "e" : 0 , "i" : 0 , "o" : 0 , "u" : 0 ,
"A" : 0 , "E" : 0 , "I" : 0 , "O" : 0 , "U" : 0 }
def replaceVowelsWithK(test_str, K):
new_string = []
string_list = list (test_str)
for char in string_list:
if char in vowels.keys():
new_string.append(K)
else :
new_string.append(char)
return (''.join(new_string))
input_str = "Geeks for Geeks"
K = "#"
print ( "Given String:" , input_str)
print ( "Given Specified Character:" , K)
print ( "After replacing vowels with the specified character:" ,
replaceVowelsWithK(input_str, K))
|
Output
Given String: Geeks for Geeks
Given Specified Character: #
After replacing vowels with the specified character: G##ks f#r G##ks
Time Complexity: O(N)
Auxiliary Space: O(N)
Method#5: Using translate () method.
Python3
test_str = "Geeks for Geeks"
K = '#'
print ( "Given String:" , test_str)
print ( "Given Specified Character:" , K)
vowels = "aeiouAEIOU"
trans = str .maketrans(vowels, K * len (vowels))
test_str = test_str.translate(trans)
print ( "After replacing vowels with the specified character:" ,
test_str)
|
Output
Given String: Geeks for Geeks
Given Specified Character: #
After replacing vowels with the specified character: G##ks f#r G##ks
Method #6 : Using ord() method
Python3
input_str = "Geeks for Geeks"
K = "#"
print ( "Given String:" , input_str)
print ( "Given Specified Character:" , K)
vow = [ 97 , 101 , 105 , 111 , 117 , 65 , 69 , 73 , 79 , 85 ]
for i in input_str:
if ord (i) in vow:
input_str = input_str.replace(i,K)
print ( "After replacing vowels with the specified character:" ,input_str)
|
Output
Given String: Geeks for Geeks
Given Specified Character: #
After replacing vowels with the specified character: G##ks f#r G##ks
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #7: Using list comprehension and join() method
This method involves using a list comprehension to iterate through each character in the input string, replacing each vowel encountered with the specified character K. Finally, the list is joined back into a string and returned as the output.
Step-by-step approach:
- Define a function replaceVowelsWithK_v2 that takes two arguments: test_str and K.
- Create a list comprehension that iterates through each character in test_str. If the character is a vowel, replace it with the specified character K; otherwise, leave it unchanged.
- Use the join() method to join the list of characters back into a string.
- Return the modified string as output.
- Test the function by calling it with sample inputs.
Python3
def replaceVowelsWithK_v2(test_str, K):
vowels = 'AEIOUaeiou'
new_str = [K if ele in vowels else ele for ele in test_str]
output_str = ''.join(new_str)
return output_str
input_str = "Geeks for Geeks"
K = "#"
print ( "Given String:" , input_str)
print ( "Given Specified Character:" , K)
print ( "After replacing vowels with the specified character:" ,
replaceVowelsWithK_v2(input_str, K))
|
Output
Given String: Geeks for Geeks
Given Specified Character: #
After replacing vowels with the specified character: G##ks f#r G##ks
Time complexity: O(n)
Auxiliary space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...