Remove vowels from a string in Python

In this tutorial, we will talk about How to remove vowels from string in Python. So in order to understand the code first, we need to understand what is a string.

What is a String?

A string is a sequence of characters. Strings are immutable (once defined, it cannot be changed). Strings are either surround by single quotation or double quotation. ( ‘  ‘ or ”   “).

String Methods in Python

  • isalnum() – if all characters in a string is numeric.
  • isalpha()- if all characters in a string is alphabet.
  • isupper() – if all characters in a string is in upper case.
  • islower() – if all characters in a string is in lower case.
  • swapcase() – converts lower case to upper case or lower case to upper case.

Basic program of string

String1 = "Hello "
 print(String1) 
Output: 
 Hello

Remove vowels from String in Python

string = input("Enter any string: ")
if string == 'x':
    exit();
else:
    newstr = string;
    print("\nRemoving vowels from the given string");
    vowels = ('a', 'e', 'i', 'o', 'u');
    for x in string.lower():
        if x in vowels:
            newstr = newstr.replace(x,"");
    print("New string after successfully removed all the vowels:");
    print(newstr);

In the above code in order to remove all vowels present in a string enter any string having vowel in it. Lets take x as string without a vowel if there is no vowel in the string the code will automatically exit but if there is a vowel in the string (‘a’,’e’,’i’,’o’,’u’)  it will remove all the vowels present in the string and will print new string (newstr) having no vowels in it.

Also learn:





Leave a Reply

Your email address will not be published. Required fields are marked *