Open In App

Python – Replace vowels by next vowel

Last Updated : 15 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)

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




# Define the input string
test_str = 'geekforgeeks'
# Print the original string
print("The original string is : " + str(test_str))
# Import the re module
import re
# Create a pattern to match vowels
pattern = '[aeiou]'
# Create a string of the next vowels
next_vowels = 'eioua'
# Create a dictionary to map vowels to the next vowel
vowel_map = dict(zip('aeiou', next_vowels))
# Define a function to replace vowels with the next vowel
def replace_vowel(match):
    return vowel_map[match.group(0)]
# Use the re.sub function to replace vowels in the input string
result = re.sub(pattern, replace_vowel, test_str)
# Print the result
print("The replaced string : " + result)
#This code is contributed by Jyothi pinjala


Output

The original string is : geekforgeeks
The replaced string : giikfurgiiks

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

Approach#6:Using list comprehension

  1. Define a list of vowels.
  2. Use a list comprehension to replace the vowels with the next vowel.
  3. Join the modified characters to form the new string.

Python3




# Function to replace the vowels
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
 
# Driver Code
test_str = 'geekforgeeks'
 
print(replace_vowels(test_str))


Output

giikfurgiiks

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

Similar Reads