In this tutorial, we are going to learn writing python program to remove all vowels present in the string.
Problem Statement
For any input string, we have to check whether the string contains vowels or not, if it does then remove the vowels and print the output.
For example:
Case1: If the user is given input ‘Quescol’
The output should be ‘Qcsl’
As we know that ‘u’, ‘e’, and ‘o’ are vowels.
Case2: If the user is given input ‘Website’
The output should be ‘Wbst’
As we know that ‘e’, and ‘i’ are vowels
Our logic to remove vowels from the string
- Our program will take any string input from the user.
- Each character of the string will be iterated using ‘for loop’ and checked for the presence of vowels in them using ‘in’ function.
- If vowels are found, replace the character with an empty string and concatenate the rest of the character.
- The result is printed as the output of our program
Python code to remove vowels from the string
#taking the input string from the user
string = input("Enter a String : ")
result=''
for i in string:
#iterating through each character of the string
if i in ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'):
#seaching for vowels
i = ''
#if vowel found replace it with empty string
result += i
#concatenate rest of the string
print("String after removing the vowels :",result)
Output
Enter a String : quescol
String after removing the vowels : qscl
Explanation
As we can observe, for the input string ‘Quescol’ the output generated is ‘Qscl’. Where the vowels ‘u’, ‘e’, and ‘o’ are removed. While iterating through the string, when vowels were found the vowels were replaced through empty string in line ‘ i= ‘’ ’, and the rest characters were concatenated.