Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python | Split String After Delimiter

#1
Python | Split String After Delimiter

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;931921&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</div>
<p><strong>Summary</strong>: You can use one of the following methods to split a string after the delimiter –</p>
<ul>
<li>Using split</li>
<li>Using string slicing</li>
<li>Using regex</li>
<li>Using partition</li>
<li>Using removeprefix</li>
</ul>
<h3>Minimal Example</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Given String
chat = "Python Founder: Guido van Rossum"
# Method 1
print(chat.split(':')[1])
# Method 2
print(chat[chat.index(":")+1:])
# Method 3
import re
print(re.findall("Sad.*)", chat)[0])
# Method 4
print(chat.partition(':')[2])
# Method 5
print(chat.removeprefix('Python Founder:'))</pre>
<h2><strong>Problem Formulation</strong></h2>
<p class="has-global-color-8-background-color has-background"> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4dc.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Problem</strong>: Given a string; How will you split the string after the delimiter? The output must only contain the substring after the delimiter.</p>
<h3>Example</h3>
<p>Let’s visualize the problem with the help of an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Input
text = "Subscribe to - Finxter"
# Expected Output
Finxter</pre>
<h2><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />Method 1: Using split()</h2>
<p><strong>Approach:</strong> First, we will simply split thestring using “-” as the delimiter. Next, to extract the string before the delimiter we will use the index of the required substring. As the <code>split()</code> function returns a list of substrings, we can extract the last part using list indexing <code>[1]</code> (Indexing starts at 0).</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text = "Subscribe to - Finxter"
res = text.split('-')[1]
print(res)
# Finxter</pre>
<p><strong>Note:</strong> The <code>split()</code> function splits the string at a given separator and returns a split list of substrings. It returns a list of the words in the string, using sep as the delimiter string.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-string-split/"><strong>Python String split()</strong></a></p>
<h2><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />Method 2: Using String Slicing</h2>
<p><strong>Prerequisite: </strong>String slicing is a concept of carving out a substring from a given string. Use slicing notation <code>s[startConfusedtopConfusedtep]</code> to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values.</p>
<p><strong>Approach:</strong> First, we will use the <code>index()</code> method to find the occurrence of the delimiter in the text. Next, we will slice the string from the index next to the index of the delimiter until the end of the string. Note that we are starting from the index of the delimiter+1 because we don’t want to include it in the final output.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text = "Subscribe to - Finxter"
res = text[text.index("-")+1:]
print(res) # Finxter</pre>
<p><strong>Note:</strong> </p>
<p>The <code>index()</code>  method<strong> </strong>is used to return the index of the first occurrence of the specified substring, like find() but it raises a ValueError if the substring is not found.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Reads: </strong><a href="https://blog.finxter.com/daily-python-puzzle-string-slicing/#:~:text=String%20slicing%20is%20a%20concept,in%20index%20stop%20(excluded)."><br /><strong>String Slicing in Python</strong></a><br /><a href="https://blog.finxter.com/python-string-index/"><strong>Python String index()</strong></a></p>
<h2><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />Method 3: Using <a href="https://blog.finxter.com/python-regex/" target="_blank" rel="noreferrer noopener">regex</a></h2>
<p>The <code>re.match(pattern, string)</code> method returns a match object if the pattern matches at the beginning of the string. The match object contains useful information such as the matching groups and the matching positions.</p>
<p><strong>Approach:</strong> Use the expression <code>re.findall("-(.*)"</code>, given_string) to match and store all characters that come after the “<code>-</code>“.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re text = "Subscribe to - Finxter"
print(re.findall("-(.*)", text)[0]) # Finxter</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read:</strong> <a href="https://blog.finxter.com/python-regex-match/"><strong>Python Regex Match</strong></a></p>
<h2><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />Method 4: Using partition</h2>
<p>The <code>partition()</code> method searches for a separator substring and returns a tuple with three strings: (1) everything before the separator, (2) the separator itself, and (3) everything after it. It then returns a tuple with the same three strings.&nbsp;</p>
<p><strong>Approach:</strong> We have used the partition method and used “-” as a separator. As we only need the substring before the delimiter, we have used the index of the required substring on the returned tuple and just printed the third element of the tuple (everything before the separator).</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text = "Subscribe to - Finxter"
res = text.partition('-')[2]
print(res) # Finxter</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-string-partition/"><strong>Python String partition()</strong></a></p>
<h2><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />Method 5: Using removeprefix</h2>
<p>If you are using Python 3.9 or above then Python facilitates you with the <code>removeprefix</code> method that allows you to remove the substring that comes before a specified substring. Here’s a quick look at how the <code>removeprefix</code> method works – </p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" decoding="async" width="858" height="234" src="https://blog.finxter.com/wp-content/uploads/2022/11/image-292.png" alt="" class="wp-image-932569" srcset="https://blog.finxter.com/wp-content/uploads/2022/11/image-292.png 858w, https://blog.finxter.com/wp-content/uplo...300x82.png 300w, https://blog.finxter.com/wp-content/uplo...68x209.png 768w" sizes="(max-width: 858px) 100vw, 858px" /><figcaption class="wp-element-caption">source: <a href="https://docs.python.org/3.9/library/stdtypes.html#str.removeprefix">https://docs.python.org/3.9/library/stdtypes.html#str.removeprefix</a></figcaption></figure>
<p><strong>Code:</strong> </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text = "Subscribe to - Finxter"
print(text.removeprefix('Subscribe to -')) # Finxter</pre>
<h2>Conclusion</h2>
<p>Hurrah! We have successfully solved the given problem using as many as five different ways. I hope you enjoyed this <a href="https://blog.finxter.com/"><strong>article</strong></a> and it helps you in your Python coding journey. Please <a href="https://blog.finxter.com/subscribe/"><strong>subscribe and stay tuned</strong></a> for more interesting articles!</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p><strong><em>Do you want to master the regex superpower?</em></strong> Check out my new book <em><strong><a href="https://blog.finxter.com/ebook-the-smartest-way-to-learn-python-regex/" target="_blank" rel="noreferrer noopener" title="[eBook] The Smartest Way to Learn Python Regex">The Smartest Way to Learn Regular Expressions in Python</a></strong></em> with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video. </p>
</div>


https://www.sickgaming.net/blog/2022/11/...delimiter/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016