PDA

View Full Version : Python question


Erik Horwath
Nov8-05, 07:02 PM
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.

Tim.thelion
Dec1-05, 11:56 PM
string = "hio"
vowels="AaEeIiOoUu"
for v in vowels:
if v in string:
string=string.replace(v," ")
print string

Hurkyl
Dec2-05, 12:00 AM
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?