1

I want to remove all the vowels from the string. but the following code doesn't work. Instead I need to put escape character before ^
i.e obj=re.compile(r'[\^aeiouAEIOU]')

import re

def disemvowel(string):
    obj=re.compile(r'[^aeiouAEIOU]')   
    k=obj.sub('',string)
    return k

s='This website is for losers LOL!'  
print( disemvowel(s) )
1
  • You actually want to remove vowels, so obj=re.compile(r'[aeiou]', re.I) Commented Apr 4, 2020 at 6:21

4 Answers 4

2

Actually your current character class [^aeiouAEIOU] will match everything except for vowels. Try this version:

s = "This website is for losers LOL!"
out = re.sub(r'[aeiou]', '', s, flags=re.IGNORECASE)
print(s + "\n" + out)

This prints:

This website is for losers LOL!
Ths wbst s fr lsrs LL!
Sign up to request clarification or add additional context in comments.

Comments

1

An easy way to do this is :

import re

string = "This website is for losers LOL!"
print(re.sub("[aeiouAEIOU]","",string))

Comments

0

To remove vowels, you match all the vowels from your string and replace it by '', so, there is no need to add ^ inside []:

import re

def disemvowel(string):
    obj=re.compile(r'[aeiou]', re.I)   
    k=obj.sub('',string)
    return k

s='This website is for losers LOL!'  
print(disemvowel(s))
# Ths wbst s fr lsrs LL!

Comments

0

Try this :

def rem_vowel(string):
    vowels = ('a', 'e', 'i', 'o', 'u')
    for x in string.lower():
        if x in vowels:
            string = string.replace(x, "")

    print(string)

string = "This website is for losers LOL!"
rem_vowel(string) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.