Program to Remove all Consonants from a String

Python Program to Remove all Consonants from a String

Given a string, the task is to remove all consonants present in the given string in Python.

Examples:

Example1:

Input:

Given string = 'HellothisisBTechGeeks '

Output:

The given string before removing consonants is [ HellothisisBTechGeeks ]
The given string after removing consonants is [ eoiieee ]

Example2:

Input:

Given string = 'goodmorningthisisbtechgeeks '

Output:

The given string before removing consonants is [ goodmorningthisisbtechgeeks ]
The given string after removing consonants is [ oooiiieee ]

Program to Remove all Consonants from a String in Python

Below are the ways to remove all consonants present in the given string in Python.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Method #1: Using remove() method (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Convert this string to a list of characters using the list() function say charsstringlist.
  • Give the vowels as the list of characters(lowercase and uppercase) say vowelslst.
  • Traverse the charsstringlist using For loop(by taking list(charsstringlist) as we remove the list elements we need a temporary list)
  • Check if the iterator character is not present in vowelslst(which means it is consonant) using not in and If operator.
  • If it is true then remove the character from the charsstringlist.
  • Join the charsstringlist using the join() function.
  • Print the string after removing the consonants.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvnstrng = 'HellothisisBTechGeeks'
print('The given string before removing consonants is [', gvnstrng, ']')
# Convert this string to a list of characters using
# the list() function say charsstringlist.
charsstringlist = list(gvnstrng)
# Give the vowels as the list of characters(lowercase and uppercase) say .
vowelslst = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
# Traverse the charsstringlist using For loop(by taking list(charsstringlist)
# as we remove the list elements we need a temporary list)
for itrchar in list(charsstringlist):
        # Check if the iterator character is not present in
    # vowelslst(which means it is consonant) using not in and If operator.
    if itrchar not in vowelslst:
        # If it is true then remove the character from the charsstringlist.
        charsstringlist.remove(itrchar)
# Join the charsstringlist using the join() function.
modifiedstrng = ''.join(charsstringlist)
# Print the string after removing the consonants.
print('The given string after removing consonants is [', modifiedstrng, ']')

Output:

The given string before removing consonants is [ HellothisisBTechGeeks ]
The given string after removing consonants is [ eoiieee ]

Method #2: Using remove() method (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Convert this string to a list of characters using the list() function say charsstringlist.
  • Give the vowels as the list of characters(lowercase and uppercase) say vowelslst.
  • Traverse the charsstringlist using For loop(by taking list(charsstringlist) as we remove the list elements we need a temporary list)
  • Check if the iterator character is not present in vowelslst(which means it is consonant) using not in and If operator.
  • If it is true then remove the character from the charsstringlist.
  • Join the charsstringlist using the join() function.
  • Print the string after removing the consonants.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvnstrng = input('Enter some random string to remove consonants = ')
print('The given string before removing consonants is [', gvnstrng, ']')
# Convert this string to a list of characters using
# the list() function say charsstringlist.
charsstringlist = list(gvnstrng)
# Give the vowels as the list of characters(lowercase and uppercase) say .
vowelslst = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
# Traverse the charsstringlist using For loop(by taking list(charsstringlist)
# as we remove the list elements we need a temporary list)
for itrchar in list(charsstringlist):
        # Check if the iterator character is not present in
    # vowelslst(which means it is consonant) using not in and If operator.
    if itrchar not in vowelslst:
        # If it is true then remove the character from the charsstringlist.
        charsstringlist.remove(itrchar)
# Join the charsstringlist using the join() function.
modifiedstrng = ''.join(charsstringlist)
# Print the string after removing the consonants.
print('The given string after removing consonants is [', modifiedstrng, ']')

Output:

Enter some random string to remove consonants = goodmorningthisisbtechgeeks
The given string before removing consonants is [ goodmorningthisisbtechgeeks ]
The given string after removing consonants is [ oooiiieee ]

Related Programs: