Remove vowels from a string

I tried to be as clear as possible in the title, but it was difficult to explain it briefly. I have to remove all vowels from a specific line; To do this, I made a loop that goes through a list of characters from this line, removes the vowels and then appends them:

def anti_vowel(text):
    vow = ["a", "e", "i", "o", "u"]
    chars = []

    for i in text:
        chars.append(i)

    for i in chars:
        if i.lower() in vow:
            chars.remove(i)

    return "".join(chars)

The problem is that when I run the code, there will always be a vowel that is not deleted. Examples:

>>> anti_vowel("Hey look Words!")
Hy lk Words!

>>> anti_vowel("Frustration is real")
Frstrton s ral

I am by no means an expert in Python, but this is confusing. Why would he delete a few letters and keep others, even if they are exactly the same?

+4
source share
3 answers

@arshajii , . , ( , , , , .)

def anti_vowel(text):
    vow = ["a", "e", "i", "o", "u"]
    chars = []

    for i in text: #No need of the two separate loops
        if i.lower() not in vow:
            chars.append(i)

    return "".join(chars)

:

>>> def anti_vowel(text):
...     vow = ["a", "e", "i", "o", "u"]
...     chars = []
...     for i in text: #No need of the two separate loops
...         if i.lower() not in vow:
...             chars.append(i)
...     return "".join(chars)
... 
>>> anti_vowel("Hey look Words!")
'Hy lk Wrds!'
>>> anti_vowel("Frustration is real") 
'Frstrtn s rl'
>>> 
+5

- , , :

import re
def anti_vowel(text):
    return re.sub("[aeiou]+", "", text)
+4

Here is a way that works:

mytext = 'hello world!'
vowels = ['a', 'e', 'i', 'o', 'u']
result = ''
for letter in mytext:
    if letter not in vowels:
        result += letter

print(result)
+3
source

Source: https://habr.com/ru/post/1537238/


All Articles