{"id":1140,"date":"2019-12-18T07:19:50","date_gmt":"2019-12-18T07:19:50","guid":{"rendered":"https:\/\/www.askpython.com\/?p=1140"},"modified":"2022-08-06T13:17:03","modified_gmt":"2022-08-06T13:17:03","slug":"remove-character-from-string-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/remove-character-from-string-python","title":{"rendered":"5 Ways to Remove a Character from String in Python"},"content":{"rendered":"\n<p>The following methods are used to remove a specific character from a string in Python.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>By using <code>Naive<\/code> method<\/li><li>By using <code>replace()<\/code> function<\/li><li>By using <code>slice<\/code> and <code>concatenation<\/code><\/li><li>By using <code>join()<\/code> and <code>list comprehension<\/code><\/li><li>By using <code>translate()<\/code> method<\/li><\/ol>\n\n\n\n<p>Note that the string is immutable in Python. So the original string remains unchanged and a new string is returned by these methods.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Removing a Character from String using the Naive method<\/h2>\n\n\n\n<p> In this method, we have to run a loop and append the characters and build a new string from the existing characters except when the index is n. (where n is the index of the character to be removed)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput_str = &quot;DivasDwivedi&quot;\n  \n# Printing original string  \nprint (&quot;Original string: &quot; + input_str) \n  \nresult_str = &quot;&quot; \n  \nfor i in range(0, len(input_str)): \n    if i != 3: \n        result_str = result_str + input_str&#x5B;i] \n  \n# Printing string after removal   \nprint (&quot;String after removal of i&#039;th character : &quot; + result_str)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>Original string: DivasDwivedi                                                                                                                 <br>String after removal of i&#8217;th character : DivsDwivedi    <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Removal of Character from a String using replace() Method<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr = &quot;Engineering&quot;\n  \n\nprint (&quot;Original string: &quot; + str) \n  \n\nres_str = str.replace(&#039;e&#039;, &#039;&#039;) \n  \n\n# removes all occurrences of &#039;e&#039; \nprint (&quot;The string after removal of character: &quot; + res_str) \n  \n# Removing 1st occurrence of e \n\nres_str = str.replace(&#039;e&#039;, &#039;&#039;, 1) \n   \nprint (&quot;The string after removal of character: &quot; + res_str) \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>Original string: Engineering                                                                                                                  <br>The string after removal of character: Enginring                                                                                              <br>The string after removal of character: Enginering    <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Removal of Character from a String using Slicing and Concatenation<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr = &quot;Engineering&quot;\n  \n\nprint (&quot;Original string: &quot; + str) \n  \n# Removing char at pos 3 \n# using slice + concatenation \nres_str = str&#x5B;:2] +  str&#x5B;3:] \n  \n\nprint (&quot;String after removal of character: &quot; + res_str) \n\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>Original string: Engineering                                                                                                                  <br>String after removal of character: Enineering <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Removal of Character from a String using join() method and list comprehension<\/h2>\n\n\n\n<p>In this technique, every element of the string is converted to an equivalent element of a list, after which each of them is joined to form a string excluding the particular character to be removed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr = &quot;Engineering&quot;\n  \n\nprint (&quot;Original string: &quot; + str) \n  \n# Removing char at index 2 \n# using join() + list comprehension \nres_str = &#039;&#039;.join(&#x5B;str&#x5B;i] for i in range(len(str)) if i != 2]) \n  \n\nprint (&quot;String after removal of character: &quot; + res_str) \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>Original string: Engineering                                                                                                                  <br>String after removal of character: Enineering <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Removal of character from a string using translate() method<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr = &#039;Engineer123Discipline&#039;\n\nprint(str.translate({ord(i): None for i in &#039;123&#039;}))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p> EngineerDiscipline <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/2.4\/lib\/string-methods.html\" target=\"_blank\" rel=\"noopener\">Python String<\/a><\/li><li>Python removal of character from a string<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The following methods are used to remove a specific character from a string in Python. By using Naive method By using replace() function By using slice and concatenation By using join() and list comprehension By using translate() method Note that the string is immutable in Python. So the original string remains unchanged and a new [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1140","post","type-post","status-publish","format-standard","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1140","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=1140"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1140\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}