Skip to content

Commit 63466ff

Browse files
updated python tip on remove string methods
1 parent dbf8f9c commit 63466ff

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

atom.xml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6994,8 +6994,8 @@ Specifying a greater than <code>0</code> start index when using
69946994
<updated>2022-05-11T00:00:00+00:00</updated>
69956995
<link rel="alternate" href="https://learnbyexample.github.io/tips/python-tip-10/" type="text/html"/>
69966996
<id>https://learnbyexample.github.io/tips/python-tip-10/</id>
6997-
<content type="html">&lt;p&gt;Python supports a lot of string methods that reduces the need for regular expressions. The &lt;code&gt;removeprefix()&lt;&#x2F;code&gt; and &lt;code&gt;removesuffix()&lt;&#x2F;code&gt; string methods were added in Python 3.9 version. See &lt;a href=&quot;https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0616&#x2F;&quot;&gt;PEP 616&lt;&#x2F;a&gt; for more details.&lt;&#x2F;p&gt;
6998-
&lt;p&gt;These methods will help you delete an exact substring from the start and end of the input string respectively. Here are some examples:&lt;&#x2F;p&gt;
6997+
<content type="html">&lt;p&gt;Python supports plenty of string methods that reduces the need for regular expressions. The &lt;code&gt;removeprefix()&lt;&#x2F;code&gt; and &lt;code&gt;removesuffix()&lt;&#x2F;code&gt; string methods were added in the Python 3.9 version. See &lt;a href=&quot;https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0616&#x2F;&quot;&gt;PEP 616&lt;&#x2F;a&gt; for more details.&lt;&#x2F;p&gt;
6998+
&lt;p&gt;These methods help to delete an exact substring from the start and end of the input string respectively. Here are some examples:&lt;&#x2F;p&gt;
69996999
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# remove &amp;#39;sp&amp;#39; if it matches at the start of the input string
70007000
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;spare&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;removeprefix&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;sp&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
70017001
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;are&amp;#39;
@@ -7007,16 +7007,20 @@ Specifying a greater than &lt;code&gt;0&lt;&#x2F;code&gt; start index when using
70077007
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# only one occurrence of the match will be removed
70087008
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;this meme&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;removesuffix&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;me&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
70097009
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;this me&amp;#39;
7010-
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# unlike strip methods, substring has to match exactly in the same order
7010+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# characters have to be matched exactly in the same order
70117011
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;this meme&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;removesuffix&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;em&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
70127012
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;this meme&amp;#39;
70137013
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
7014-
&lt;p&gt;These remove methods will delete the given substring only once from the start or end of the string. On the other hand, the strip methods treat the argument as a set of characters to be matched any number of times in any order until a non-matching character is found. Here&#x27;s an example:&lt;&#x2F;p&gt;
7014+
&lt;p&gt;These remove methods will delete the given substring only once from the start or end of the string. On the other hand, the strip methods treat the argument as a set of characters to be matched any number of times in any order until a non-matching character is found. Here are some examples:&lt;&#x2F;p&gt;
70157015
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;these memes&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;removesuffix&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;esm&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
70167016
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;these memes&amp;#39;
7017-
&lt;&#x2F;span&gt;&lt;span&gt;
70187017
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;these memes&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;rstrip&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;esm&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
70197018
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;these &amp;#39;
7019+
&lt;&#x2F;span&gt;&lt;span&gt;
7020+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;effective&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;removeprefix&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;ef&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
7021+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;fective&amp;#39;
7022+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;effective&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;lstrip&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;ef&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
7023+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;ctive&amp;#39;
70207024
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
70217025
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
70227026
&lt;p align=&quot;center&quot;&gt;&lt;iframe width=&quot;560&quot; height=&quot;315&quot; loading=&quot;lazy&quot; src=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;b--A6OM3PyI&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;&#x2F;iframe&gt;&lt;&#x2F;p&gt;

tips/python-tip-10/index.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Python tip 10: removeprefix and removesuffix string methods</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content="Python tip 10: removeprefix and removesuffix string methods" property=og:title><meta content=website property=og:type><meta content="The removeprefix and removesuffix string methods will delete an exact substring from the start and end of the input string respectively." property=og:description><meta content=https://learnbyexample.github.io/python-tip-10/ property=og:url><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/tips/python-tip-10/>Python tip 10: removeprefix and removesuffix string methods</a></h1><div class=post__meta><span class=post__time>2022-05-11</span></div></header><div class=post-content><p>Python supports a lot of string methods that reduces the need for regular expressions. The <code>removeprefix()</code> and <code>removesuffix()</code> string methods were added in Python 3.9 version. See <a href=https://peps.python.org/pep-0616/>PEP 616</a> for more details.<p>These methods will help you delete an exact substring from the start and end of the input string respectively. Here are some examples:<pre class=language-python data-lang=python style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-python data-lang=python><span style=color:#7f8989;># remove 'sp' if it matches at the start of the input string
1+
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Python tip 10: removeprefix and removesuffix string methods</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content="Python tip 10: removeprefix and removesuffix string methods" property=og:title><meta content=website property=og:type><meta content="The removeprefix and removesuffix string methods will delete an exact substring from the start and end of the input string respectively." property=og:description><meta content=https://learnbyexample.github.io/python-tip-10/ property=og:url><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/tips/python-tip-10/>Python tip 10: removeprefix and removesuffix string methods</a></h1><div class=post__meta><span class=post__time>2022-05-11</span></div></header><div class=post-content><p>Python supports plenty of string methods that reduces the need for regular expressions. The <code>removeprefix()</code> and <code>removesuffix()</code> string methods were added in the Python 3.9 version. See <a href=https://peps.python.org/pep-0616/>PEP 616</a> for more details.<p>These methods help to delete an exact substring from the start and end of the input string respectively. Here are some examples:<pre class=language-python data-lang=python style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-python data-lang=python><span style=color:#7f8989;># remove 'sp' if it matches at the start of the input string
22
</span><span style=color:#72ab00;>>>> </span><span style=color:#d07711;>'spare'</span><span>.</span><span style=color:#5597d6;>removeprefix</span><span>(</span><span style=color:#d07711;>'sp'</span><span>)
33
</span><span style=color:#d07711;>'are'
44
</span><span style=color:#7f8989;># 'par' is present in the input, but not at the start
@@ -9,12 +9,16 @@
99
</span><span style=color:#7f8989;># only one occurrence of the match will be removed
1010
</span><span style=color:#72ab00;>>>> </span><span style=color:#d07711;>'this meme'</span><span>.</span><span style=color:#5597d6;>removesuffix</span><span>(</span><span style=color:#d07711;>'me'</span><span>)
1111
</span><span style=color:#d07711;>'this me'
12-
</span><span style=color:#7f8989;># unlike strip methods, substring has to match exactly in the same order
12+
</span><span style=color:#7f8989;># characters have to be matched exactly in the same order
1313
</span><span style=color:#72ab00;>>>> </span><span style=color:#d07711;>'this meme'</span><span>.</span><span style=color:#5597d6;>removesuffix</span><span>(</span><span style=color:#d07711;>'em'</span><span>)
1414
</span><span style=color:#d07711;>'this meme'
15-
</span></code></pre><p>These remove methods will delete the given substring only once from the start or end of the string. On the other hand, the strip methods treat the argument as a set of characters to be matched any number of times in any order until a non-matching character is found. Here's an example:<pre class=language-python data-lang=python style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-python data-lang=python><span style=color:#72ab00;>>>> </span><span style=color:#d07711;>'these memes'</span><span>.</span><span style=color:#5597d6;>removesuffix</span><span>(</span><span style=color:#d07711;>'esm'</span><span>)
15+
</span></code></pre><p>These remove methods will delete the given substring only once from the start or end of the string. On the other hand, the strip methods treat the argument as a set of characters to be matched any number of times in any order until a non-matching character is found. Here are some examples:<pre class=language-python data-lang=python style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-python data-lang=python><span style=color:#72ab00;>>>> </span><span style=color:#d07711;>'these memes'</span><span>.</span><span style=color:#5597d6;>removesuffix</span><span>(</span><span style=color:#d07711;>'esm'</span><span>)
1616
</span><span style=color:#d07711;>'these memes'
17-
</span><span>
1817
</span><span style=color:#72ab00;>>>> </span><span style=color:#d07711;>'these memes'</span><span>.</span><span style=color:#5597d6;>rstrip</span><span>(</span><span style=color:#d07711;>'esm'</span><span>)
1918
</span><span style=color:#d07711;>'these '
19+
</span><span>
20+
</span><span style=color:#72ab00;>>>> </span><span style=color:#d07711;>'effective'</span><span>.</span><span style=color:#5597d6;>removeprefix</span><span>(</span><span style=color:#d07711;>'ef'</span><span>)
21+
</span><span style=color:#d07711;>'fective'
22+
</span><span style=color:#72ab00;>>>> </span><span style=color:#d07711;>'effective'</span><span>.</span><span style=color:#5597d6;>lstrip</span><span>(</span><span style=color:#d07711;>'ef'</span><span>)
23+
</span><span style=color:#d07711;>'ctive'
2024
</span></code></pre><p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/b--A6OM3PyI width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See also my <a href=https://github.com/learnbyexample/100_page_python_intro>100 Page Python Intro</a> ebook.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/python/>#python</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=previous href=https://learnbyexample.github.io/tips/python-tip-11/>← Python tip 11: capture external command output</a><br><p><a class=next href=https://learnbyexample.github.io/tips/python-tip-9/>Python tip 9: applying set-like operations for dictionaries →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>

0 commit comments

Comments
 (0)