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 surrounded by single quotations or double quotations ( ‘ ‘ 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 = '''Thank you for learning from CodeSpeedy. THANK YOU FOR LEARNING FROM CODESPEEDY.''' newstr = string print("\nRemoving vowels from the given string"); vowels = ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') for x in string: if x in vowels: newstr = newstr.replace(x,"") print("New string after successfully removed all the vowels:") print(newstr)
Output :
Removing vowels from the given string New string after successfully removed all the vowels: Thnk y fr lrnng frm CdSpdy. THNK Y FR LRNNG FRM CDSPDY.
In the above code, if there is a vowel (‘a’,’e’,’i’,’o’,’u’,’A’,’E’,’I’,’O’,’U’) in the string then it will remove all the vowels present in the string and print the new string (newstr
).
Also learn:
Thank you for learning from CodeSpeedy. If you have any doubts, feel free to ask them in the comments section below.
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