w3resource

Python: Remove all vowels from a given string

Python Basic - 1: Exercise-128 with Solution

Write a Python program to remove all vowels from a given string.

Sample Solution-1:

Python Code:

import re
def test(text):
    return re.sub(r'[aeiou]+', '', text, flags=re.IGNORECASE)
text = "Python";
print("Original string:",text)
print("After removing all the vowels from the said string: " + test(text))
text = "C Sharp"
print("\nOriginal string:",text)
print("After removing all the vowels from the said string: " + test(text))
text = "JavaScript"
print("\nOriginal string:",text)
print("After removing all the vowels from the said string: " + test(text))

Sample Output:

Original string: Python
After removing all the vowels from the said string: Pythn

Original string: C Sharp
After removing all the vowels from the said string: C Shrp

Original string: JavaScript
After removing all the vowels from the said string: JvScrpt

Flowchart:

Flowchart: Python - Remove all vowels from a given string.

Sample Solution-2:

Python Code:

def test(text):
    vowels = "AEIOUaeiou"
    return ''.join(el for el in text if not el in vowels)

text = "Python";
print("Original string:",text)
print("After removing all the vowels from the said string: " + test(text))
text = "C Sharp"
print("\nOriginal string:",text)
print("After removing all the vowels from the said string: " + test(text))
text = "JavaScript"
print("\nOriginal string:",text)
print("After removing all the vowels from the said string: " + test(text))

Sample Output:

Original string: Python
After removing all the vowels from the said string: Pythn

Original string: C Sharp
After removing all the vowels from the said string: C Shrp

Original string: JavaScript
After removing all the vowels from the said string: JvScrpt

Flowchart:

Flowchart: Python - Remove all vowels from a given string.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to check whether the average value of the elements of a given array of numbers is a whole number or not.
Next: Write a Python program to get the index number of all lower case letters in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.