The code excludes vowels from a string. The code works, and print() does the job, but I need to return the result. I tried .join()
but it didn’t work: it returns only the first character in my case, because the return stops the loop. How can I overcome this?
JavaScript
x
9
1
def remove_vowels(document: str) -> str:
2
vowels = "aeioyu"
3
document = document.lower()
4
for chart in document:
5
if chart not in vowels:
6
print(''.join(chart), end = '' )
7
return (''.join(chart))
8
remove_vowels("document")
9
Advertisement
Answer
Try correcting your indentation:
JavaScript
1
10
10
1
def remove_vowels(text: str) -> str:
2
vowels = set("aeioyu")
3
text_without_vowels = []
4
for chart in text:
5
if chart.lower() not in vowels:
6
text_without_vowels.append(chart)
7
return ''.join(text_without_vowels)
8
9
print(remove_vowels("document"))
10
You could also consider utilizing a comprehension:
JavaScript
1
6
1
def remove_vowels(text: str) -> str:
2
vowels = set("aeioyu")
3
return ''.join(chart for chart in text if chart.lower() not in vowels)
4
5
print(remove_vowels("document"))
6
Output:
JavaScript
1
2
1
dcmnt
2