Skip to main content

How to remove all vowels from a string in Python

How to remove all vowels from a string in Python.

Here's a step-by-step tutorial on how to remove all vowels from a string in Python.

  1. First, let's define the string from which we want to remove the vowels. For example, let's say our string is "Hello, World!".
string = "Hello, World!"
  1. Next, let's create an empty string variable to store the string without vowels. We'll call it new_string.
new_string = ""
  1. Now, we'll iterate through each character in the original string using a for loop.
for char in string:
  1. Inside the loop, we'll check if the character is a vowel. We can do this by using an if statement and the lower() method to convert the character to lowercase.
if char.lower() not in "aeiou":
  1. If the character is not a vowel, we'll add it to the new_string variable using the concatenation operator +=.
new_string += char
  1. Finally, after the loop ends, we'll print or return the new_string variable, which will contain the original string without any vowels.
print(new_string)

Putting it all together, the complete code to remove all vowels from a string looks like this:

string = "Hello, World!"
new_string = ""

for char in string:
if char.lower() not in "aeiou":
new_string += char

print(new_string)

When you run this code, it will output: "Hll, Wrld!" since all the vowels ('e', 'o', 'o') have been removed from the original string.

You can modify this code to suit your specific needs. For example, instead of printing the result, you can store it in a variable or use it in further calculations.