Python Program to Remove Vowels from a String
Beginner · Strings
This program removes the vowels a, e, i, o and u, both capital and small, from a string and keeps everything else. It builds a new string, because strings in Python cannot be changed in place.
Program
Original string: Python Programming is Fun Without vowels: Pythn Prgrmmng s Fn
How it works
vowelsholds all ten vowel characters, small and capital, so both kinds are removed.- The loop adds a character to
resultonly when it is not invowels. - Spaces, consonants, digits and punctuation pass the check, so they stay.
- The original
textis not changed: strings are immutable, so the program builds a new one.
Other ways to write it
Using a comprehension and join()
Without vowels: Pythn Prgrmmng s Fn
ch.lower() lets one check cover both cases, and "".join() turns the kept characters back into a string.
Using re.sub()
Without vowels: Pythn Prgrmmng s Fn
In a regular expression [aeiou] means any one of these letters. re.sub() replaces each match with an empty string, and re.IGNORECASE makes it match capitals too.
Try it yourself
Change the program to replace each vowel with * instead of removing it.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
- Count Each Vowel in a String
- Remove Punctuation from a String
- Check if a Character Is a Vowel or Consonant
- Count Vowels, Consonants, Uppercase and Lowercase Letters
- Check a Palindrome String
- Reverse a String
- Generate a Random Password
- Count the Frequency of Each Character in a String
- Count the Words in a String
All Python programs · CBSE class 12 practical file · Python exercises