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.



Python: Tips of the Day

What is the difference between Python's list methods append and extend?

append: Appends object at the end.

x = [1, 2, 3]
x.append([4, 5])
print (x)

Output:

[1, 2, 3, [4, 5]]

extend: Extends list by appending elements from the iterable.

x = [1, 2, 3]
x.extend([4, 5])
print (x)

Output:

[1, 2, 3, 4, 5]

Ref: https://bit.ly/2AZ6ZFq