Editor

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

remove_vowels.py
text = "Python Programming is Fun" # or: text = input("Enter a string: ")
vowels = "aeiouAEIOU"
result = ""
for ch in text:
if ch not in vowels:
result += ch
print("Original string:", text)
print("Without vowels:", result)
Output
Original string: Python Programming is Fun
Without vowels: Pythn Prgrmmng s Fn

How it works

  1. vowels holds all ten vowel characters, small and capital, so both kinds are removed.
  2. The loop adds a character to result only when it is not in vowels.
  3. Spaces, consonants, digits and punctuation pass the check, so they stay.
  4. The original text is not changed: strings are immutable, so the program builds a new one.

Other ways to write it

Using a comprehension and join()

Using a comprehension and join()
text = "Python Programming is Fun"
result = "".join(ch for ch in text if ch.lower() not in "aeiou")
print("Without vowels:", result)
Output
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()

Using re.sub()
import re
text = "Python Programming is Fun"
result = re.sub("[aeiou]", "", text, flags=re.IGNORECASE)
print("Without vowels:", result)
Output
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.

All Python programs · CBSE class 12 practical file · Python exercises