Create an account


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

#1
Python | Split String by Underscore

<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;942245&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 class="has-background" style="background-color:#b5b8f6"><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;" /><strong>Summary: </strong>Use <code>"given string".split()</code> to split the given string by underscore and store each word as an individual item in a list.</p>
<h3><strong>Minimal Example</strong></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="">text = "Welcome_to_the_world_of_Python"
# Method 1
print(text.split("_")) # Method 2
import re
print(re.split('_', text)) # Method 3
print(list(filter(None, text.split('_')))) # Method 4
print([x for x in re.findall(r'[^_]*|(?!_).*$', text) if x != '']) # OUTPUT: ['Welcome', 'to', 'the', 'world', 'of', 'Python']</pre>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<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 into a list of words using the underscore as a delimiter?</p>
<h3><strong>Example</strong></h3>
<p>Let’s understand 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 = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
# Output:
['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']</pre>
<p>Now without any further ado, let’s dive into the numerous ways of solving this problem.</p>
<h2><strong>Method 1: Using split()</strong></h2>
<p>Python’s built-in <code>split()</code> function splits the string at a given separator and returns a split list of substrings. Here’s how the <code>split()</code> function works: <code>'finxterx42'.split('x')</code> will split the string with the character ‘<code>x</code>’ as the delimiter and return the following list as an output: <code>['fin', 'ter', '42']</code>.</p>
<p><strong>Approach: </strong>To split a string by underscore, you need to use the underscore as the delimiter. You can simply pass the underscore as a separator to the <code>split('_')</code> function.</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 = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(text.split("_")) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30f.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><strong>Method 2: Using re.split()</strong></h2>
<p>Another way of separating a string by using the underscore as a separator is to use the <code>re.split()</code> method from the regex library. The <code>re.split(pattern, string)</code> method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, <code>re.split('a', 'bbabbbab')</code> results in the list of strings <code>['bb', 'bbb', 'b']</code>.</p>
<p><strong>Approach: </strong>You can simply use the <code>re.split()</code> method as <code>re.split('_', text)</code> where <code>'_'</code> returns a match whenever the string contains an underscore. Whenever any underscore is encountered, the text gets separated at that point.</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 = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(re.split('_', text)) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-regex-split/"><strong>Python Regex Split</strong></a></p>
<h2><strong>Method 3: Using filter()</strong></h2>
<p>Python’s built-in <code>filter()</code> function filters out the elements that pass a filtering condition. It takes two arguments: function and iterable. The function assigns a Boolean value to each element in the iterable to check whether the element will pass the filter or not. It returns an iterator with the elements that passes the filtering condition.</p>
<p><strong>Approach: </strong>You can use the <code>filter()</code> method to split the string by underscore. The function takes None as the first argument and the list of split strings as the second argument. The <code>filter()</code> function iterates through the list and removes any empty elements. As the <code>filter()</code> method returns an object, we need to use the <code>list()</code> to convert the object into a list.</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 = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(list(filter(None, text.split('_')))) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-filter/"><strong>Python filter()</strong></a></p>
<h2><strong>Method 4: Using re.findall()</strong></h2>
<p>The <code>re.findall(pattern, string)</code> method scans the string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order- when scanning the string from left to right.</p>
<p><strong>Approach: </strong>You can use the <code>re.findall()</code> method from the regex module to split the string by underscore. You can use ‘<code>[^_]</code>‘ as the pattern that can be fed into the <code>findall</code> function to solve the problem. It simply, means a set all characters that start with an underscore will be grouped together.</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 = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print([x for x in re.findall(r'[^_]*', text) if x != '']) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-re-findall/"><strong>Python re.findall() – Everything You Need to Know</strong></a></p>
<h2><strong>Python | Split String by Dot</strong></h2>
<p>Now that we have gone through numerous ways of solving the given problem, here’s a similar programming challenge for you to solve.</p>
<p><strong>Challenge: </strong>You are given a string that contains dots in it. How will you split the string using a dot as a delimiter? Consider the code below and try to split the string by dot.</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 = "stars.moon.sun.sky" # Expected Output:
['stars', 'moon', 'sun', 'sky']</pre>
<p>Try to solve the problem yourself before looking into the given solutions.</p>
<p><strong>Solution: </strong>Here are the different methods to split a string by using the dot as a delimiter/separator:</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 = "stars.moon.sun.sky"
# Method 1
print(text.split(".")) # Method 2
print(list(filter(None, text.split('.')))) # Method 3
import re
print([x for x in re.findall(r'[^.]*|(?!.).*$', text) if x != '']) # Method 4
print(re.split('\\.', text))</pre>
<h2>Conclusion</h2>
<p>Hurrah! We have successfully solved the given problem using as many as four 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/12/...nderscore/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016