So I am trying to solve a problem on the PyBites Platform where it asks you to do the following:
- Take any string
- Format it to lowercase
- Replace any vowel with the * symbol
- Track how many vowels were changed
The example string text = 'Hello World'
should return the following tuple: ('h*ll* w*rld', 3)
with 3 representing the total vowels changed.
The code below includes a function that should take care of all the steps listed. I even used assignment so that I could use .replace() and output the changed characters:
from typing import Tuple
text = 'Hello World'
def strip_vowels(text: str) -> Tuple[str, int]:
vowels = 'aeiou'
count = 0
text = text.lower().splitlines()
for words in text:
for char in words:
for vowel in vowels:
if char == vowel:
count += 1
result = words.replace(char, '*'), count
return result
answer = strip_vowels(text)
print(answer)
The problem I am having is that although I am successfully checking whether a character in a string is a vowel or not, the return value is off: ('hell* w*rld', 3)
I know that replace() is checking for a vowel at each iteration but it's not storing all the results.
Any guidance on what steps I should take? Thanks in advance.