Reputation: 11
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)
Upvotes: 0
Views: 159
Reputation: 130
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)
Upvotes: -1
Reputation: 32244
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'
Upvotes: 2