All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Remove Vowels From A String In Python

Last Updated : Mar 11, 2024

How To Remove Vowels From A String In Python

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
  1. First, a string named str1 is defined and assigned a value.
  2. In an array, we place all the vowels in the English alphabet.
  3. 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.
  4. Using a 'for' loop, we will iterate over the given string.
  5. Through iteration, each character can be checked if it is a vowel or not.
  6. To check if it is a vowel, we simply use the keywords 'not' and 'in'.
  7. Here, if the character is not in the 'vowels' array, it means it is a consonant.
  8. So, we use the join() function to add the characters to the result string.
  9. The separator in the join() function is an empty string so that the sequence of characters in a string doesn't get altered.
  10. Finally, we can print the result string which doesn't contain any vowels!
  11. Considering the example taken in the above sample code, the string 'Hello World, I am learning Python' is defined as str1.
  12. A list of vowels and an empty string to store the final answer are defined on the next lines respectively.
  13. 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!
  14. If it is not a vowel then it is joined to the result string.
  15. For example, the first iteration will be of 'H', it is not a vowel so it is joined to the result string.
  16. Also, the next iteration is of the 'e' character, it is a vowel so the if statement will not get executed.
  17. 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.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪