Comp Sci Troubleshooting a Vowel-Removing & Duplicating Function

AI Thread Summary
A user is seeking help to create two functions: one that removes vowels from a string and another that duplicates each vowel. The initial attempt at the removeVowels function does not modify the string correctly because it does not update the new string in each iteration of the loop. Suggestions include ensuring that the replace method operates on the updated string rather than the original. Additionally, there is a question about the necessity of checking if a vowel is present in the string before replacing it. The discussion emphasizes the need for proper string manipulation to achieve the desired outcomes.
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:

Similar threads

Replies
4
Views
1K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
7
Views
3K
Replies
2
Views
4K
Replies
3
Views
7K
Replies
9
Views
3K
Replies
6
Views
3K
Back
Top