Python – Replace vowels by next vowel
Last Updated :
15 Apr, 2023
Given a String replace each vowel with next vowel in series.
Input : test_str = ‘geekforgeeks’
Output : giikfurgiiks
Explanation : After e, next vowel is i, all e replaced by i.
Input : test_str = ‘geekforgeeks is best’
Output : giikfurgiiks os bist
Explanation : After e, next vowel is i, all e replaced by i.
Method #1 : Using zip() + list comprehension
This is one of the ways in which this task can be performed. In this we perform the task of forming replace dictionary using zip() and then list comprehension is used to perform the task of replacement with next vowel.
Python3
test_str = 'geekforgeeks'
print ( "The original string is : " + str (test_str))
vow = 'a e i o u' .split()
temp = dict ( zip (vow, vow[ 1 :] + [vow[ 0 ]]))
res = "".join([temp.get(ele, ele) for ele in test_str])
print ( "The replaced string : " + str (res))
|
Output
The original string is : geekforgeeks
The replaced string : giikfurgiiks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using dictionary comprehension + list comprehension
This is yet another way in which this task can be performed. In this we perform the task of mapping using dictionary comprehension and list comprehension is used to perform task of replacement.
Python3
test_str = 'geekforgeeks'
print ( "The original string is : " + str (test_str))
vow = "aeiou"
temp = {vow[idx] : vow[(idx + 1 ) % len (vow)] for idx in range ( len (vow))}
res = "".join([temp.get(ele, ele) for ele in test_str])
print ( "The replaced string : " + str (res))
|
Output
The original string is : geekforgeeks
The replaced string : giikfurgiiks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using index() method
Python3
test_str = 'geekforgeeks'
print ( "The original string is : " + str (test_str))
vow = "aeiou"
res = ""
for i in test_str:
if i in vow:
if i = = "u" :
res + = "a"
else :
res + = vow[vow.index(i) + 1 ]
else :
res + = i
print ( "The replaced string : " + str (res))
|
Output
The original string is : geekforgeeks
The replaced string : giikfurgiiks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using translate() method
This method uses the translate() method to replace the vowels in the input string. First, it creates a dictionary using the maketrans() method with the vowels as the keys and the next vowels as the values. Then it uses the translate() method to replace the vowels in the input string with the corresponding next vowels from the dictionary.
Python3
test_str = 'geekforgeeks'
print ( "The original string is : " + str (test_str))
vow = 'aeiou'
vow_dict = str .maketrans( 'aeiou' , 'eioua' )
result = test_str.translate(vow_dict)
print ( "The replaced string : " + str (result))
|
Output
The original string is : geekforgeeks
The replaced string : giikfurgiiks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: Without using built-in functions
Python3
test_str = 'geekforgeeks'
vowels = 'aeiou'
result = ''
for char in test_str:
if char in vowels:
index = vowels.index(char)
if index = = len (vowels) - 1 :
result + = vowels[ 0 ]
else :
result + = vowels[index + 1 ]
else :
result + = char
print (result)
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#6: using re.sub() function:
First, the input string is defined and printed to the console. The “re” module is then imported to use regular expression functions.
A pattern is created using square brackets to match any vowel in the input string. Then, a string is defined with the next vowel for each vowel in the English alphabet.
A dictionary is created using the “zip” function to map each vowel to its corresponding next vowel.
Then, a function is defined to replace each matched vowel with its corresponding next vowel using the “re.sub” function, which takes the pattern, the function to replace the pattern, and the input string as arguments.
The function returns the value of the key in the dictionary that matches the pattern group. The resulting string is stored in a variable and printed to the console.
Python3
test_str = 'geekforgeeks'
print ( "The original string is : " + str (test_str))
import re
pattern = '[aeiou]'
next_vowels = 'eioua'
vowel_map = dict ( zip ( 'aeiou' , next_vowels))
def replace_vowel(match):
return vowel_map[match.group( 0 )]
result = re.sub(pattern, replace_vowel, test_str)
print ( "The replaced string : " + result)
|
Output
The original string is : geekforgeeks
The replaced string : giikfurgiiks
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach#6:Using list comprehension
- Define a list of vowels.
- Use a list comprehension to replace the vowels with the next vowel.
- Join the modified characters to form the new string.
Python3
def replace_vowels(test_str):
vowels = 'aeiou'
new_str = ''.join([vowels[vowels.index(char) + 1 ] if char in vowels and char ! =
'u' else 'a' if char = = 'u' else char for char in test_str])
return new_str
test_str = 'geekforgeeks'
print (replace_vowels(test_str))
|
Time complexity: O(n), where n is the length of the input string.
Space complexity: O(n), where n is the length of the input string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...