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:
what does “.lower” in “string.lower()” do there?
It changes every upper case alphabet into lower case.
s = ‘XYZ’
s.lower() returns ‘xyz’
s= ‘XYZ’
s = s.lower()
print(s)
> xyz
like this
yes but is not doing that operation(of converting upper to lower)
s= ‘XYZ’
s = s.lower()
print(s)
>> xyz
.lower() change every upper case alphabet into lower case.
but what if i want to remove both upper case and lower case vowels, and not just the lowercase,how can i do that?
That explanation is bad I think.
If the input is exactly “x” it EXITS the program.
If the string is anything else it runs the routine:
Makes all upper case lower case
Checks for vowels
Substitutes a empty blank for any vowel in the string
Outputs what part(s) of the string that is not a vowel.
str = input(“Enter any string: “)
newstr = str.lower() # Convert all charcters into Lowercase
vowels = (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) # vowels as Tuples
for i in str.lower():
if i in vowels:
newstr = newstr.replace(i, “”) # Replaced by no balank spaces
print(“New string after successfully removed all the vowels: “)
print(newstr)
is it necessary that we have to write lower in the program
Won’t work. Strings are immutable. first convert string to list, carry out your process and convert back to string.
s= input()
vowels=”aeiouAEIOU”
new_s=s
for x in s:
if x in vowels:
new_s=new_s.replace(x,””)
print (new_s)
s=input(“”)
new_s = s
for x in s :
if x in ‘aeiouAEIOU’:
new_s = new_s.replace(x,””)
print(new_s)
Capital letters are not removing.
Example: Once upon a time
Output: Onc pn tm
I want to remove “Upper Case” letters also.
Kindly remove that line : for x in string.lower():
And add capital letters too in this list: vowels = (‘a’, ‘e’, ‘i’, ‘o’, ‘u’);
It will work fine