{"id":47783,"date":"2023-03-30T08:42:30","date_gmt":"2023-03-30T08:42:30","guid":{"rendered":"https:\/\/www.askpython.com\/?p=47783"},"modified":"2023-03-30T08:42:31","modified_gmt":"2023-03-30T08:42:31","slug":"remove-vowels-from-string","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/remove-vowels-from-string","title":{"rendered":"How to remove vowels from string in Python?"},"content":{"rendered":"\n<p>A string is a data type that can consist of any combination of letters, numbers, and symbols. A string can contain both vowels and consonants of alphabets borrowed from the language of English. In this article, we are going to remove vowels from a string using Python. There are different techniques which can be deployed to get this done in Python viz.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>The Loop Method<\/strong><\/li>\n\n\n\n<li><strong>Using the Regular Expression<\/strong><\/li>\n\n\n\n<li><strong>The Iterator &#038; Join Technique<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Each listed method works on an exclusive logic to define the input string and then remove the vowels associated with it. In simpler terms, these techniques make use of the loops or formulations to check and remove the listed vowels from the given string and then return the output string without any vowels.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method I &#8211; The Loop Method<\/strong><\/h2>\n\n\n\n<p>This method would loop around in search of the vowels in the given input string. The notable point here is that we include both uppercase &#038; lowercase vowels while declaring what to detect in the string. This is done to factor in the effect of difference cases in which the letters are available in the string. <\/p>\n\n\n\n<p>We shall get started by assigning <em>hippopotamus <\/em>as an input string, followed by listing down all the vowels to be identified and removed from the string. Also we shall define a variable <em>result <\/em>to be empty.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring = &quot;hippopotamus&quot;\nvowels = {&#039;a&#039;, &#039;e&#039;, &#039;i&#039;, &#039;o&#039;, &#039;u&#039;, &#039;A&#039;, &#039;E&#039;, &#039;I&#039;, &#039;O&#039;, &#039;U&#039;}\nresult = &quot;&quot;\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"659\" height=\"64\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Assigning-Input-String-and-Vowels.jpg\" alt=\"Assigning Input String And Vowels\" class=\"wp-image-47788\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Assigning-Input-String-and-Vowels.jpg 659w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Assigning-Input-String-and-Vowels-300x29.jpg 300w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><figcaption class=\"wp-element-caption\">Assigning Input String and Vowels<\/figcaption><\/figure>\n\n\n\n<p>Now we shall create an iteration using a <em><a href=\"https:\/\/www.askpython.com\/course\/python-course-for-loop\" data-type=\"post\" data-id=\"18443\">for loop<\/a><\/em> combined with an <em>if<\/em> statement to extract the result as string without vowels. Have a look at its construction below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor i in range(len(string)):\n    if string&#x5B;i] not in vowels:\n        result = result + string&#x5B;i]\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"75\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Constructing-Vowel-removal-code.jpg\" alt=\"Constructing Vowel Removal Code\" class=\"wp-image-47792\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Constructing-Vowel-removal-code.jpg 631w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Constructing-Vowel-removal-code-300x36.jpg 300w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><figcaption class=\"wp-element-caption\">Constructing the Vowel Removal Code<\/figcaption><\/figure>\n\n\n\n<p>Once done, the <em>print <\/em>function shall be used to return the result.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\n After removing Vowels:&quot;, result)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"51\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Print-function-to-return-the-result.jpg\" alt=\"Print Function To Return The Result\" class=\"wp-image-47793\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Print-function-to-return-the-result.jpg 604w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Print-function-to-return-the-result-300x25.jpg 300w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><figcaption class=\"wp-element-caption\">Displaying the Result<\/figcaption><\/figure>\n\n\n\n<p>When the code is run, the following result is returned.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"513\" height=\"66\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Resultant-string-without-vowels.jpg\" alt=\"Resultant String Without Vowels\" class=\"wp-image-47794\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Resultant-string-without-vowels.jpg 513w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Resultant-string-without-vowels-300x39.jpg 300w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><figcaption class=\"wp-element-caption\">Resultant String Without Vowels<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method II \u2013 Using the Regular Expression<\/strong>s<\/h2>\n\n\n\n<p>An alternate technique that can be deployed to remove the vowels from the input string is using the \u2018re\u2019 \u2013 regular expression. We will get started by importing it from Python&#8217;s standard library, followed by defining a customized module. Within this module we shall provide the condition that states how to remove the vowels from the input string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport re\ndef rem_vowel(string):\n    return(re.sub(&quot;&#x5B;aeiouAEIOU]&quot;,&quot;&quot;,string))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"699\" height=\"86\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Creating-a-Custom-Function.jpg\" alt=\"Creating A Custom Function\" class=\"wp-image-47797\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Creating-a-Custom-Function.jpg 699w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Creating-a-Custom-Function-300x37.jpg 300w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><figcaption class=\"wp-element-caption\">Creating a Custom Function to Remove Vowels<\/figcaption><\/figure>\n\n\n\n<p>Declare an input string &#038; return the output using the <em>print <\/em>statement as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring = &quot;Hippopotamus&quot;\nx = rem_vowel(string)\nprint(x)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"219\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Result-removing-vowels-1024x219.jpg\" alt=\"Result Removing Vowels\" class=\"wp-image-47800\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Result-removing-vowels-1024x219.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Result-removing-vowels-300x64.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Result-removing-vowels-768x164.jpg 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Result-removing-vowels.jpg 1043w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Result Removing Vowels<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method III \u2013 The Iterator &amp; Join Technique<\/strong><\/h2>\n\n\n\n<p>This is a two step method in which the extraction of the consonants from the string is isolated at the first step followed by the joining of consonants together to form a word. Let\u2019s get started by defining the string &amp; vowels as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"471\" height=\"47\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Declaring-Vowels-Input-String.jpg\" alt=\"Declaring Vowels Input String\" class=\"wp-image-47801\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Declaring-Vowels-Input-String.jpg 471w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Declaring-Vowels-Input-String-300x30.jpg 300w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><figcaption class=\"wp-element-caption\">Declaring Vowels Input String<\/figcaption><\/figure>\n\n\n\n<p>This is followed by constructing a variable \u2018<em>result<\/em>\u2019 with a set of instructions to remove the vowels and return only the consonants from the input string. But, it is to be noted that the returned value will not be in the form of a word, rather be in the form of separated letters as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nresult = &#x5B;letter for letter in string if letter.lower() not in vowels]\nprint(result)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"790\" height=\"67\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Isolated-Consonants-Returned.jpg\" alt=\"Isolated Consonants Returned\" class=\"wp-image-47802\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Isolated-Consonants-Returned.jpg 790w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Isolated-Consonants-Returned-300x25.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Isolated-Consonants-Returned-768x65.jpg 768w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><figcaption class=\"wp-element-caption\">Isolated Consonants Returned<\/figcaption><\/figure>\n\n\n\n<p>Now it\u2019s time to join the isolated letters using join command as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfinal = &#039;&#039;.join(result)\nprint(final)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"296\" height=\"77\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Combining-Consonants-using-the-Join-function.jpg\" alt=\"Combining Consonants Using The Join Function\" class=\"wp-image-47803\"\/><figcaption class=\"wp-element-caption\">Combining Consonants Using The Join Function<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>We&#8217;ve explored three different techniques to remove vowels from a given input string using Python. Each method offers a unique approach, from using loops and conditionals to employing regular expressions or the iterator and join techniques. Having a variety of methods available will allow you to choose the most suitable approach, depending on your specific text processing needs. Continue learning and experimenting with Python to level up your skills and expand your toolkit for tackling various challenges. You may also be interested in learning how to <a href=\"https:\/\/www.askpython.com\/python\/examples\/convert-hex-to-rgb\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/examples\/convert-hex-to-rgb\" target=\"_blank\" rel=\"noreferrer noopener\">convert Hexadecimal to RGB values<\/a> in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reference:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/re.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/3\/library\/re.html<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/wiki.python.org\/moin\/ForLoop\" target=\"_blank\" rel=\"noopener\">https:\/\/wiki.python.org\/moin\/ForLoop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A string is a data type that can consist of any combination of letters, numbers, and symbols. A string can contain both vowels and consonants of alphabets borrowed from the language of English. In this article, we are going to remove vowels from a string using Python. There are different techniques which can be deployed [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":47808,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-47783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/47783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=47783"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/47783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/47808"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=47783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=47783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=47783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}