Python – Replace Consonants by i, Vowels by j
Last Updated :
20 Feb, 2023
Given a String, perform the replacement of all the vowels with i, and all the consonants using j.
Input : test_str = ‘geeksforgeeks’, i, j = “A”, “B”
Output : BAABBBABBAABB
Explanation : All vowels replaced by A and consonants by B.
Input : test_str = ‘gfg’, i, j = “A”, “B”
Output : BBB
Explanation : Only consonants present and replaced by B.
Method #1 : Using sub() + regex
In this, we use sub-function and pass regex for consonants and vowel to perform an appropriate replacement.
Python3
import re
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
i, j = "V" , "C"
res = re.sub( "[^aeiouAEIOU]" , j, test_str)
res = re.sub( "[aeiouAEIOU]" , i, res)
print ( "The string after required replacement : " + str (res))
|
Output
The original string is : geeksforgeeks
The string after required replacement : CVVCCCVCCVVCC
Time complexity: O(n), where n is the length of the input string “test_str”.
Auxiliary space: O(1). It uses only a few extra variables (i, j, res) to store intermediate results.
Method #2 : Using maketrans() + symmetric difference
In this, we first get consonants using symmetric difference of vowels and maketrans is function that is used to perform the task of replacement of Strings.
Python3
import string
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
i, j = "V" , "C"
Vows = 'aeiouAEIOU'
Cons = ''.join( set (string.ascii_letters).difference( set (Vows)))
translation = str .maketrans(Vows + Cons, i * len (Vows) + j * len (Cons))
res = test_str.translate(translation)
print ( "The string after required replacement : " + str (res))
|
Output
The original string is : geeksforgeeks
The string after required replacement : CVVCCCVCCVVCC
Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(1), since no extra data structure is used.
Method #3: Using replace()
Initialize a string with vowels.Iterate a for loop on the given string and use in operator to check whether character is in vowel string or not.If True then use replace() method to replace the character by j, if False replace the character by i.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
i, j = "V" , "C"
vow = "aeiouAEIOU"
for k in test_str:
if k in vow:
test_str = test_str.replace(k,i)
else :
test_str = test_str.replace(k,j)
res = test_str
print ( "The string after required replacement : " + str (res))
|
Output
The original string is : geeksforgeeks
The string after required replacement : CVVCCCVCCVVCC
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using for loop
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
i, j = "V" , "C"
vow = "aeiouAEIOU"
res = ""
for k in test_str:
if k in vow:
res + = i
else :
res + = j
print ( "The string after required replacement : " + str (res))
|
Output
The original string is : geeksforgeeks
The string after required replacement : CVVCCCVCCVVCC
Time complexity: O(n) where n is the length of the input string ‘test_str’.
Auxiliary space: O(n) where n is the length of the input string ‘test_str’, as we are creating a new string ‘res’ to store the result.
Method#5: Using list comprehension.
You can use a list comprehension to loop through the string and check for each character if it’s a vowel or consonant. If it’s a vowel, append “V” to a new list and if it’s a consonant append “C” to the new list. Finally, join the list to get the modified string.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
i,j = 'V' , 'C'
res = " ".join([i if c in " aeiouAEIOU" else j for c in test_str])
print ( "The string after required replacement : " + str (res))
|
Output
The original string is : geeksforgeeks
The string after required replacement : CVVCCCVCCVVCC
Time complexity: O(n), where n is the length of the input string ‘test_str’.
Auxiliary space: O(n), as a new string ‘res’ is created of length n.
Method #6 : Using ord() and replace() methods
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
i, j = "V" , "C"
vow = [ 97 , 101 , 105 , 111 , 117 , 65 , 69 , 73 , 79 , 85 ]
for k in test_str:
if ord (k) in vow:
test_str = test_str.replace(k,i)
else :
test_str = test_str.replace(k,j)
res = test_str
print ( "The string after required replacement : " + str (res))
|
Output
The original string is : geeksforgeeks
The string after required replacement : CVVCCCVCCVVCC
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7 : Using operator.countOf() method
Python3
import operator as op
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
i, j = "V" , "C"
vowels = "aeiouAEIOU"
res = ""
for k in test_str:
if op.countOf(vowels,k)> 0 :
res + = i
else :
res + = j
print ( "The string after required replacement : " + str (res))
|
Output
The original string is : geeksforgeeks
The string after required replacement : CVVCCCVCCVVCC
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...