How to delete consonants from a string in Python?


Here are some code examples to delete consonants from a string in Python:

Using for loop and if statement

One way to delete consonants from a string in Python is to loop through each character in the string and check if it is a vowel or not. Here's how you can do it:

Example

In the first line of code, we define a string called "my_string" that contains the text "Hello World".

In the second line of code, we define a string called "vowels" that contains all the vowels (both uppercase and lowercase) in the English alphabet.

In the third line of code, we initialize an empty string called "new_string".

In the fourth line of code, we loop through each character in the string using a for loop.

In the fifth line of code, we check if the character is a vowel by using the in operator to see if it is in the "vowels" string.

If the character is a vowel, we add it to the "new_string" using the += operator, which concatenates the character to the end of the string.

Finally, we print the new string to the console.

# Define a string
my_string = "Lorem Ipsum"

# Define a list of vowels
vowels = "AEIOUaeiou"

# Initialize an empty string
new_string = ""

# Loop through each character in the string
for char in my_string:
    # Check if the character is a vowel
    if char in vowels:
        # If it's a vowel, add it to the new string
        new_string += char

# Print the new string
print(new_string)

Output

oeIu

Using list comprehension

Another way to delete consonants from a string in Python is to use a list comprehension to filter out the consonants. Here's how you can do it:

Example

In the first three lines of code, we define the string "my_string", the string "vowels" that contains all the vowels, and an empty list called "new_list".

In the fourth line of code, we use a list comprehension to loop through each character in the string and check if it is a vowel or not.

If the character is a vowel, it is added to the "new_list".

In the fifth line of code, we convert the list back to a string using the join() method.

The join() method concatenates all the characters in the list and returns a new string.

Finally, we print the new string to the console.

# Define a string
my_string = "Lorem Ipsum"
# Define a list of vowels

vowels = "AEIOUaeiou"

# Use a list comprehension to filter out the consonants
new_list = [char for char in my_string if char in vowels]

# Convert the list back to a string
new_string = "".join(new_list)

# Print the new string
print(new_string)

Output

oeIu

Using regular expressions

Another way to delete consonants from a string in Python is to use regular expressions to replace all consonants with an empty string. Here's how you can do it:

Example

In the first line of code, we import the re module, which provides support for regular expressions.

In the second line of code, we define the string "my_string".

In the third line of code, we define a regular expression pattern called "consonants_pattern" that matches any character that is not a vowel.

In the fourth line of code, we use the sub() method from the re module to replace all the consonants in the string with an empty string.

The sub() method takes three arguments: the regular expression pattern to match, the replacement string (in this case an empty string), and the original string to search.

Finally, we print the new string to the console.

import re

# Define a string
my_string = "Lorem Ipsum"

# Define a regular expression pattern for consonants
consonants_pattern = "[^AEIOUaeiou]"

# Use the sub() method to replace all consonants with an empty string
new_string = re.sub(consonants_pattern, "", my_string)

# Print the new string
print(new_string)

Output

oeIu

Using lambda function and filter()

Another way to delete consonants from a string in Python is to use a lambda function with the filter() function to filter out the consonants. Here's how you can do it:

Example

In the first three lines of code, we define the string "my_string", the string "vowels" that contains all the vowels, and an empty list called "new_list".

In the fourth line of code, we use the filter() function along with a lambda function to filter out the consonants in the string.

The lambda function takes a character as an argument and returns True if it is a vowel, and False otherwise.

In the fifth line of code, we convert the filter object back to a list.

In the sixth line of code, we convert the list back to a string using the join() method.

Finally, we print the new string to the console.

# Define a string
my_string = "Lorem Ipsum"

# Define a list of vowels
vowels = "AEIOUaeiou"

# Use filter() and lambda function to filter out the consonants
new_list = list(filter(lambda char: char in vowels, my_string))

# Convert the list back to a string
new_string = "".join(new_list)

# Print the new string
print(new_string)

Output

oeIu

Using list comprehension and string method

Another way to delete consonants from a string in Python is to use a list comprehension to filter out the consonants and then use the join() method to convert the list back to a string. Here's how you can do it:

Example

In the first three lines of code, we define the string "my_string", the string "vowels" that contains all the vowels, and an empty list called "new_list".

In the fourth line of code, we use a list comprehension to loop through each character in the string and check if it is a vowel or not.

If the character is a vowel, it is added to the "new_list".

In the fifth line of code, we convert the list back to a string using the join() method.

The join() method concatenates all the characters in the list and returns a new string.

Finally, we print the new string to the console.

# Define a string
my_string = "Lorem Ipsum"

# Define a string of vowels
vowels = "AEIOUaeiou"

# Use a list comprehension to filter out the consonants
new_list = [char for char in my_string if char in vowels]

# Convert the list back to a string using join() method
new_string = "".join(new_list)

# Print the new string
print(new_string)

Output

oeIu

I hope these additional examples provide you with more ways to delete consonants from a string in Python!

Updated on: 10-Aug-2023

424 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements