You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<content type="html"><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>
6998
-
<p>These methods will help you delete an exact substring from the start and end of the input string respectively. Here are some examples:</p>
6997
+
<content type="html"><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>
6998
+
<p>These methods help to delete an exact substring from the start and end of the input string respectively. Here are some examples:</p>
6999
6999
<pre data-lang="python" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-python "><code class="language-python" data-lang="python"><span style="color:#7f8989;"># remove &#39;sp&#39; if it matches at the start of the input string
<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:</p>
7014
+
<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:</p>
<!doctype html><htmllang=en><head><metacontent="IE=edge" http-equiv=X-UA-Compatible><metacontent="text/html; charset=utf-8" http-equiv=content-type><metacontent="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Python tip 10: removeprefix and removesuffix string methods</title><linkhref=https://learnbyexample.github.io/atom.xmlrel=alternatetitle=RSStype=application/atom+xml><scriptsrc=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><linkhref=https://learnbyexample.github.io/site.cssrel=stylesheet><metacontent="Python tip 10: removeprefix and removesuffix string methods" property=og:title><metacontent=websiteproperty=og:type><metacontent="The removeprefix and removesuffix string methods will delete an exact substring from the start and end of the input string respectively." property=og:description><metacontent=https://learnbyexample.github.io/python-tip-10/property=og:url><metacontent=@learn_byexampleproperty=twitter:site><linkhref=https://learnbyexample.github.io/favicon.svgrel=icon><linkrel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><divclass=container><divclass=mobile-navbarid=mobile-navbar><divclass=mobile-header-logo><aclass=logohref=/>learnbyexample</a></div><divclass="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><navclass="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ulclass=mobile-menu-list><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/books> Books </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/mini> Mini </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/tips> Tips </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/tags> Tags </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/about> About </a></ul></nav><headerid=header><divclass=logo><ahref=https://learnbyexample.github.io>learnbyexample</a></div><navclass=menu><ul><li><ahref=https://learnbyexample.github.io/books> Books </a><li><ahref=https://learnbyexample.github.io/mini> Mini </a><li><ahref=https://learnbyexample.github.io/tips> Tips </a><li><ahref=https://learnbyexample.github.io/tags> Tags </a><li><ahref=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><divclass=contentid=mobile-panel><articleclass=post><headerclass=post__header><h1class=post__title><ahref=https://learnbyexample.github.io/tips/python-tip-10/>Python tip 10: removeprefix and removesuffix string methods</a></h1><divclass=post__meta><spanclass=post__time>2022-05-11</span></div></header><divclass=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 <ahref=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:<preclass=language-pythondata-lang=pythonstyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-pythondata-lang=python><spanstyle=color:#7f8989;># remove 'sp' if it matches at the start of the input string
1
+
<!doctype html><htmllang=en><head><metacontent="IE=edge" http-equiv=X-UA-Compatible><metacontent="text/html; charset=utf-8" http-equiv=content-type><metacontent="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Python tip 10: removeprefix and removesuffix string methods</title><linkhref=https://learnbyexample.github.io/atom.xmlrel=alternatetitle=RSStype=application/atom+xml><scriptsrc=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><linkhref=https://learnbyexample.github.io/site.cssrel=stylesheet><metacontent="Python tip 10: removeprefix and removesuffix string methods" property=og:title><metacontent=websiteproperty=og:type><metacontent="The removeprefix and removesuffix string methods will delete an exact substring from the start and end of the input string respectively." property=og:description><metacontent=https://learnbyexample.github.io/python-tip-10/property=og:url><metacontent=@learn_byexampleproperty=twitter:site><linkhref=https://learnbyexample.github.io/favicon.svgrel=icon><linkrel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><divclass=container><divclass=mobile-navbarid=mobile-navbar><divclass=mobile-header-logo><aclass=logohref=/>learnbyexample</a></div><divclass="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><navclass="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ulclass=mobile-menu-list><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/books> Books </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/mini> Mini </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/tips> Tips </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/tags> Tags </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/about> About </a></ul></nav><headerid=header><divclass=logo><ahref=https://learnbyexample.github.io>learnbyexample</a></div><navclass=menu><ul><li><ahref=https://learnbyexample.github.io/books> Books </a><li><ahref=https://learnbyexample.github.io/mini> Mini </a><li><ahref=https://learnbyexample.github.io/tips> Tips </a><li><ahref=https://learnbyexample.github.io/tags> Tags </a><li><ahref=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><divclass=contentid=mobile-panel><articleclass=post><headerclass=post__header><h1class=post__title><ahref=https://learnbyexample.github.io/tips/python-tip-10/>Python tip 10: removeprefix and removesuffix string methods</a></h1><divclass=post__meta><spanclass=post__time>2022-05-11</span></div></header><divclass=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 <ahref=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:<preclass=language-pythondata-lang=pythonstyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-pythondata-lang=python><spanstyle=color:#7f8989;># remove 'sp' if it matches at the start of the input string
</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:<preclass=language-pythondata-lang=pythonstyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-pythondata-lang=python><spanstyle=color:#72ab00;>>>></span><spanstyle=color:#d07711;>'these memes'</span><span>.</span><spanstyle=color:#5597d6;>removesuffix</span><span>(</span><spanstyle=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:<preclass=language-pythondata-lang=pythonstyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-pythondata-lang=python><spanstyle=color:#72ab00;>>>></span><spanstyle=color:#d07711;>'these memes'</span><span>.</span><spanstyle=color:#5597d6;>removesuffix</span><span>(</span><spanstyle=color:#d07711;>'esm'</span><span>)
</span></code></pre><p><strong>Video demo</strong>:<palign=center><iframeallow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreenframeborder=0height=315loading=lazysrc=https://www.youtube.com/embed/b--A6OM3PyIwidth=560></iframe></p><br><p><imgalt=infosrc=/images/info.svg> See also my <ahref=https://github.com/learnbyexample/100_page_python_intro>100 Page Python Intro</a> ebook.</div><divclass=post-footer><divclass=post-tags><ahref=https://learnbyexample.github.io/tags/python/>#python</a><ahref=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hrcolor=#e6e6e6><divclass=post-nav><p><aclass=previoushref=https://learnbyexample.github.io/tips/python-tip-11/>← Python tip 11: capture external command output</a><br><p><aclass=nexthref=https://learnbyexample.github.io/tips/python-tip-9/>Python tip 9: applying set-like operations for dictionaries →</a><br></div><hrcolor=#e6e6e6><p>📰 Use <ahref=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <ahref=https://twitter.com/learn_byexample>Twitter</a>, <ahref=https://github.com/learnbyexample>GitHub</a> and <ahref=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <ahref=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hrcolor=#e6e6e6></div></article></div></main></div><scriptsrc=https://learnbyexample.github.io/even.js></script>
0 commit comments