Function to remove vowels or duplicate vowels in a string

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
Erik Horwath
Messages
7
Reaction score
0
I need to write a function that takes an input string and returns it minus all vowels and another that returns it with each vowel duplicated. ie - in the first case, removeVowels("Zippy") would return "Zppy" and in the second case duplicateVowels("Zippy") would return "Ziippy". I know it is something obvious, but it won't come to me. I have been trying things along the line of:

def removeVowels(string):
vowels="AaEeIiOoUu"
for v in vowels:
if v in string:
newstring=string.replace(v," ")
print newstring

But i need some way to modify the statement newstring=string.replace(v," ") such that for each iteration in the for loop the dot replace method operates on the newstring from the previous iteration, not the original string. That is, in the current format removeVowels("lightbulb") returns "lightblb" not "lghtblb" Thanks.
 
Physics news on Phys.org
string = "hio"
vowels="AaEeIiOoUu"
for v in vowels:
if v in string:
string=string.replace(v," ")
print string
 
I'm not sure if you meant to say return or not, so I feel the need to point out that your function returns None, and not a string.

P.S. is there any particular reason why you're testing if v is in string?
 
Last edited: