Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Replace vowels by next vowel

Improve Article
Save Article
  • Last Updated : 08 Feb, 2023
Improve Article
Save Article

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




# Python3 code to Replace vowels by next vowel
# Using list comprehension + zip()
 
# initializing string
test_str = 'geekforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# constructing dictionary using zip()
vow = 'a e i o u'.split()
temp = dict(zip(vow, vow[1:] + [vow[0]]))
 
# list comprehension to perform replacement
res = "".join([temp.get(ele, ele) for ele in test_str])
 
# printing result
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




# Python3 code to Replace vowels by next vowel
# Using list comprehension + dictionary comprehension
 
# initializing string
test_str = 'geekforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# constructing dictionary using dictionary comprehension
vow = "aeiou"
temp = {vow[idx] : vow[(idx + 1) % len(vow)] for idx in range(len(vow))}
 
# using get() to map elements to dictionary and join to convert
res = "".join([temp.get(ele, ele) for ele in test_str])
 
# printing result
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




# Python3 code to
# Replace vowels by next vowel
 
# initializing string
test_str = 'geekforgeeks'
 
# printing original string
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
 
# printing result
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




# Python3 code to
# Replace vowels by next vowel
 
# initializing string
test_str = 'geekforgeeks'
# printing original string
print("The original string is : " + str(test_str))
 
vow = 'aeiou'
vow_dict = str.maketrans('aeiou','eioua')
result = test_str.translate(vow_dict)
 
# printing result
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)
#This code is contributed by Vinay Pinjala.

Output

giikfurgiiks

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!