Home > OS >  Replace vowels with vowel letter vowel Python
Replace vowels with vowel letter vowel Python

Time:09-22

I need to replace vowels with the existing vowel "r" vowel (eg.: input: "burger" output: "bururgerer").

It would mean this, but I need to write a program that recognizes the given vowel and repeats it.

x = str(input("Please add text: "))  
y = x.replace("a","ara").replace("e", "ere").replace("i", "iri")  
print(y)

CodePudding user response:

You can use re.sub to match any vowel and reference the matched vowel in the replacement string by using a group (\1)

import re
re.sub('(a|e|i|o|u)', r'\1r\1', 'burger') # 'bururgerer'

CodePudding user response:

You can loop through the input and check each character to see if it's a vowel, and then add the 'r' as needed.

x = str(input("Input word: "))
vowels = ['a', 'e', 'i', 'o', 'u']
y = ''

for letter in range(len(x.lower())):
    if x[letter] in vowels:
        y  = x[letter]   'r'   x[letter]
    else:
        y  = x[letter]

print("\n\nOld: ",x)
print("\n\nNew: ",y)
  • Related