{"id":13244,"date":"2021-09-30T12:30:55","date_gmt":"2021-09-30T07:00:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=13244"},"modified":"2021-11-22T18:34:37","modified_gmt":"2021-11-22T13:04:37","slug":"python-program-to-remove-all-consonants-from-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-all-consonants-from-a-string\/","title":{"rendered":"Python Program to Remove all Consonants from a String"},"content":{"rendered":"
Given a string, the task is to remove all consonants present in the given string in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to remove all consonants present in the given string in Python.<\/p>\n Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples<\/a><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nGiven string = 'HellothisisBTechGeeks '<\/pre>\n
The given string before removing consonants is [ HellothisisBTechGeeks ]\r\nThe given string after removing consonants is [ eoiieee ]<\/pre>\n
Given string = 'goodmorningthisisbtechgeeks '<\/pre>\n
The given string before removing consonants is [ goodmorningthisisbtechgeeks ]\r\nThe given string after removing consonants is [ oooiiieee ]<\/pre>\n
Program to Remove all Consonants from a String in Python<\/h2>\n
\n
Method #1: Using remove() method (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvnstrng = 'HellothisisBTechGeeks'\r\nprint('The given string before removing consonants is [', gvnstrng, ']')\r\n# Convert this string to a list of characters using\r\n# the list() function say charsstringlist.\r\ncharsstringlist = list(gvnstrng)\r\n# Give the vowels as the list of characters(lowercase and uppercase) say .\r\nvowelslst = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']\r\n# Traverse the charsstringlist using For loop(by taking list(charsstringlist)\r\n# as we remove the list elements we need a temporary list)\r\nfor itrchar in list(charsstringlist):\r\n # Check if the iterator character is not present in\r\n # vowelslst(which means it is consonant) using not in and If operator.\r\n if itrchar not in vowelslst:\r\n # If it is true then remove the character from the charsstringlist.\r\n charsstringlist.remove(itrchar)\r\n# Join the charsstringlist using the join() function.\r\nmodifiedstrng = ''.join(charsstringlist)\r\n# Print the string after removing the consonants.\r\nprint('The given string after removing consonants is [', modifiedstrng, ']')\r\n<\/pre>\n
The given string before removing consonants is [ HellothisisBTechGeeks ]\r\nThe given string after removing consonants is [ eoiieee ]<\/pre>\n
Method #2: Using remove() method (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = input('Enter some random string to remove consonants = ')\r\nprint('The given string before removing consonants is [', gvnstrng, ']')\r\n# Convert this string to a list of characters using\r\n# the list() function say charsstringlist.\r\ncharsstringlist = list(gvnstrng)\r\n# Give the vowels as the list of characters(lowercase and uppercase) say .\r\nvowelslst = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']\r\n# Traverse the charsstringlist using For loop(by taking list(charsstringlist)\r\n# as we remove the list elements we need a temporary list)\r\nfor itrchar in list(charsstringlist):\r\n # Check if the iterator character is not present in\r\n # vowelslst(which means it is consonant) using not in and If operator.\r\n if itrchar not in vowelslst:\r\n # If it is true then remove the character from the charsstringlist.\r\n charsstringlist.remove(itrchar)\r\n# Join the charsstringlist using the join() function.\r\nmodifiedstrng = ''.join(charsstringlist)\r\n# Print the string after removing the consonants.\r\nprint('The given string after removing consonants is [', modifiedstrng, ']')\r\n<\/pre>\n
Enter some random string to remove consonants = goodmorningthisisbtechgeeks\r\nThe given string before removing consonants is [ goodmorningthisisbtechgeeks ]\r\nThe given string after removing consonants is [ oooiiieee ]<\/pre>\n
\n