Troubleshooting a Vowel-Removing & Duplicating Function

Click For Summary
SUMMARY

The forum discussion centers on creating two Python functions: one to remove vowels from a string and another to duplicate vowels within a string. The user provided an initial implementation of the removeVowels function but encountered issues with the replacement logic, resulting in incorrect outputs. The solution involves modifying the loop to ensure that each iteration operates on the updated string rather than the original input. The discussion also highlights the importance of returning the modified string instead of printing it directly.

PREREQUISITES
  • Understanding of Python programming language
  • Familiarity with string manipulation methods in Python
  • Knowledge of function definition and return statements in Python
  • Basic understanding of loops and conditionals in programming
NEXT STEPS
  • Learn about Python string methods, specifically replace() and join()
  • Research how to use list comprehensions for string manipulation in Python
  • Explore the concept of recursion in Python for more advanced string processing
  • Investigate unit testing in Python to validate function outputs
USEFUL FOR

Python developers, programming students, and anyone interested in string manipulation techniques in Python.

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 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K