Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Replace vowels in a string with a specific character K

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 31 Jan, 2023
Improve Article
Save Article

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




# Function to Replace each vowel in
# the string with a specified character
def replaceVowelsWithK(test_str, K):
 
    # string of vowels
    vowels = 'AEIOUaeiou'
 
    # iterating to check vowels in string
    for ele in vowels:
 
        # replacing vowel with the specified character
        test_str = test_str.replace(ele, K)
 
    return test_str
 
 
   
# Driver Code
# input string
input_str = "Geeks for Geeks"
 
# specified character
K = "#"
 
# printing input
print("Given String:", input_str)
print("Given Specified Character:", K)
 
# printing output
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

Output:

Given String: Geeks for Geeks 
Given Specified Character: # 
After replacing vowels with the specified character: G##ks f#r G##ks

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




# Function to Replace each vowel
# in the string with a specified character
def replaceVowelsWithK(test_str, K):
 
    # creating list of vowels
    vowels_list = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
 
    # creating empty list
    new_string = []
 
    # converting the given string to list
    string_list = list(test_str)
 
    # running 1st iteration for
    # comparing all the
    # characters of string with
    # the vowel characters
    for char in string_list:
 
        # running 2nd iteration for
        # comparing all the characters
        # of vowels with the string character
        for char2 in vowels_list:
 
            # comparing string character
            # and vowel character
            if char == char2:
 
                # if condition is true then adding
                # the specific character entered
                # by the user in the new list
                new_string.append(K)
                break
 
        # else adding the character
        else:
            new_string.append(char)
 
    # return the converted list into string
    return(''.join(new_string))
 
   
 
# Driver Code
# input string
input_str = "Geeks for Geeks"
 
# specified character
K = "#"
 
# printing input
print("Given String:", input_str)
print("Given Specified Character:", K)
 
# printing output
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#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




# Function to Replace each vowel in
# the string with a specified character
import re
def replaceVowelsWithK(test_str, K):
 
    # string of vowels
    vowels = 'AEIOUaeiou'
    return re.sub(rf'[{vowels}]', K, test_str)
 
 
 
# Driver Code
# input string
input_str = "Geeks for Geeks"
 
# specified character
K = "&"
 
# printing input
print("Given String:", input_str)
print("Given Specified Character:", K)
 
# printing output
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




# Function to Replace each vowel
# in the string with a specified character
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):
    # creating empty list
    new_string = []
 
    # converting the given string to list
    string_list = list(test_str)
 
    # running 1st iteration for
    # comparing all the
    # characters of string with
    # the vowel characters
    for char in string_list:
 
        if char in vowels.keys():
            new_string.append(K)
 
        # else adding the character
        else:
            new_string.append(char)
 
    # return the converted list into string
    return(''.join(new_string))
 
 
# Driver Code
# input string
input_str = "Geeks for Geeks"
 
# specified character
K = "#"
 
# printing input
print("Given String:", input_str)
print("Given Specified Character:", K)
 
# printing output
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)

Space Complexity: O(N)

Method#5: Using translate () method.

Python3




#Initializing Test_str
test_str = "Geeks for Geeks"
#Declaring specified character
K='#'
#Printing original string
print("Given String:", test_str)
#printing Specified Character
print("Given Specified Character:", K)
 
#Declaring Vowels
vowels = "aeiouAEIOU"
#str.maketrans() method creates a translation table that replaces vowels in the string with "#".
trans = str.maketrans(vowels, K*len(vowels))
#The translate() method then applies this translation table to the original string and replaces the vowels with "#.
test_str = test_str.translate(trans)
#printing output
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




# Program to Replace each vowel in
# the string with a specified character
 
# input string
input_str = "Geeks for Geeks"
 
# specified character
K = "#"
 
# printing input
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)
# printing output
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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!