﻿ Python: Remove all vowels from a given string - 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:

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:

Python Code Editor:

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

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