How To Remove Vowels From A String In Python
Last Updated : Mar 11, 2024
In this article we will show you the solution of how to remove vowels from a string in python, often we have to edit the strings as per our requirements. Here, we are going to remove vowels from a string in Python.
Strings are immutable, that is they cannot be updated, then how can we alter the string according to our needs?
The answer to our question is we can't alter or modify the string.
When we perform any altering functions on a string, a new string is formed in the backend, and the previous one is erased from the memory.
Step By Step Guide On How To Remove Vowels From A String In Python :-
There are several methods to solve a given problem. Here, as we have to remove vowels from the string; a string can be a combination of vowels and consonants.
To remove the vowels from a string in Python, we will use for-loop and built-in lower() and join() functions.
lower() function:
Syntax: str.lower()
Use: The lower() function converts all the characters in a string into lowercase of the alphabet.
join() function:
Syntax: separator_string.join(iterable item)
Use: The join() function takes all items in an iterable item and joins them into one string.
A string is specified as the separator.
str1 = 'Hello World, I am learning Python' vowels = ['a', 'e', 'i', 'o', 'u'] result = "" for i in str1: if i.lower() not in vowels: result += ''.join(i) print(result) #Code Output Hll Wrld, m lrnng Pythn
- First, a string named str1 is defined and assigned a value.
- In an array, we place all the vowels in the English alphabet.
- Also, have declared another empty string variable named result, which will have our final result and in the end, we will print this result variable.
- Using a 'for' loop, we will iterate over the given string.
- Through iteration, each character can be checked if it is a vowel or not.
- To check if it is a vowel, we simply use the keywords 'not' and 'in'.
- Here, if the character is not in the 'vowels' array, it means it is a consonant.
- So, we use the join() function to add the characters to the result string.
- The separator in the join() function is an empty string so that the sequence of characters in a string doesn't get altered.
- Finally, we can print the result string which doesn't contain any vowels!
- Considering the example taken in the above sample code, the string 'Hello World, I am learning Python' is defined as str1.
- A list of vowels and an empty string to store the final answer are defined on the next lines respectively.
- A for loop is used to iterate through every character in the string and the 'if' condition statement is used to check if the character is a vowel or not!
- If it is not a vowel then it is joined to the result string.
- For example, the first iteration will be of 'H', it is not a vowel so it is joined to the result string.
- Also, the next iteration is of the 'e' character, it is a vowel so the if statement will not get executed.
- Similarly, all the iterations occur till the end of the string, and the result string is obtained.
Conclusion :-
This is how we can remove the vowels from a string in Python with the help of built-in functions like lower() and join() and some basic fundamentals like for loop and if conditions.
I hope this article on how to remove vowels from a string in python helps you and the steps and method mentioned above are easy to follow and implement.