I am attempting to replace multiple letters within a string, I want the vowels to be replaced by the users input, and my current code replaces all the vowels with the same letters, however I want to replace the vowels with different user inputs. Below is an example of what I would like, and the code below that.
What I want
input1 = zz
input2 = xx
input3 = yolo
output = yzzlxx
What I have
input1 = zz
input2 = xx
input3 = yolo
output = yzzlzz
Here is my code.
def vwl():
syl1 = input("Enter your first syllable: ")
syl2 = input("Enter the second syllable: ")
translate = input("Enter word to replace vowels in: ")
for ch in ['a','e','i','o','u']:
if ch in translate:
translate=translate.replace(ch,syl1,)
for ch in ['a','e','i','o','u']:
if syl1 in translate:
translate=translate.replace(ch,syl2,)
print (translate)
The method replace
takes an additional argument count
:
translate=translate.replace(ch,syl1,1)
break # finish the for loop for syl1
will only replace the first instance of ch and break will ensure you don't replace any subsequent vowels with syl1
.
Similarly:
translate=translate.replace(ch,syl2,1)
break # finish the for loop