Create an account


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

#1
Python | Split String Variable Spaces

<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;957593&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:#d1a9fa"><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> The most efficient way to split a string using variable spaces is to use the <code>split</code> function like so <code>given_string.split()</code>. An alternate approach is to use different functions of the regex package to split the string at multiple whitespaces.</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 = "a b c d"
# Method 1
print(text.split())
# Method 2
import re
print(re.split('\s+', text))
# Method 3
print([x for x in re.findall(r'\S+', text) if x != ''])
# Method 4
print(re.sub(r'\s+', ',', text).split(','))
# Method 5
print(list(filter(None, text.split()))) # ['a', 'b', 'c', 'd']</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 using multiple spaces?</p>
<h3><strong>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=""># Input
text = "abc xyz lmn pqr"
# Output
['abc', 'xyz', 'lmn', 'pqr']</pre>
<p>The given input has multiple spaces between each substring, i.e., there are three spaces after <code>abc</code>, two spaces after <code>xyz</code> while a single space after <code>lmn</code>. So, not only do you have multiple spaces between the substring but also varied number of spaces. Can you split the string by varied and multiple spaces?</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p>Though the question might look daunting at first but once you get hold of it, the solutions to this problem are easier than one can imagine. So, without further delay let us dive into the different ways of solving the given problem.</p>
<h2>Method 1: Using&nbsp;<a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-split/" target="_blank">split()</a></h2>
<p>The built-in <code>split('sep')</code> function allows you to split a string in Python based on a given delimiter. By default the&nbsp;<code>split</code>&nbsp;function splits a given string at whitespaces. Meaning, if you do not pass any delimiter to the split function then the string will be split at whitespaces. </p>
<p>You can use this default property of the split function and successfully split the given string at multiple spaces just by using the&nbsp;<code>split()</code>&nbsp;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 = "abc xyz lmn pqr"
print(text.split()) # ['abc', 'xyz', 'lmn', 'pqr']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4da.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Recommended Digest</strong>:&nbsp;<strong><a href="https://blog.finxter.com/python-string-split/">Python String split()</a></strong></p>
<h2>Method 2: <strong>Using re.split</strong></h2>
<p>The&nbsp;<code>re.split(pattern, string)</code>&nbsp;method matches all occurrences of the&nbsp;<code>pattern</code>&nbsp;in the&nbsp;<code>string</code>&nbsp;and divides the string along the matches resulting in a list of strings&nbsp;<em>between&nbsp;</em>the matches. For example,&nbsp;<code>re.split('a', 'bbabbbab')</code>&nbsp;results in the list of strings&nbsp;<code>['bb', 'bbb', 'b']</code>.</p>
<p><strong>Approach:&nbsp;</strong>To split the string using multiple space characters use&nbsp;<code>re.split("\s+", text)</code>&nbsp;where&nbsp;<code>\s+</code>&nbsp;is the matching pattern and it represents a special sequence that returns a match whenever it finds any whitespace character and splits the string. So, whenever there’s a space or multiple spaces (any number of occurrences of space are whitespace characters) the string will be split. </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 = "abc xyz lmn pqr"
print(re.split('\s+', text))
# ['abc', 'xyz', 'lmn', 'pqr']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4da.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Recommended Read: &nbsp;<strong><a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex-split/" target="_blank">Python Regex Split</a></strong>.</strong></p>
<h2><strong>Method 3</strong>: Using re.findall</h2>
<p>The&nbsp;<code>re.findall(pattern, string)</code>&nbsp;method scans the&nbsp;<code>string</code>&nbsp;from&nbsp;<strong>left to right</strong>, searching for all&nbsp;<strong>non-overlapping matches</strong>&nbsp;of the&nbsp;<code>pattern</code>. It returns a&nbsp;<strong>list of strings</strong>&nbsp;in the matching order when scanning the string from left to right.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4da.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Recommended Read:&nbsp;<a rel="noreferrer noopener" href="https://blog.finxter.com/python-re-findall/" target="_blank">Python re.findall() – Everything You Need to Know</a></strong></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 = "abc xyz lmn pqr"
print([x for x in re.findall(r'\S+', text) if x != ''])
# ['abc', 'xyz', 'lmn', 'pqr']</pre>
<h2><strong>Method 4: Using <a href="https://blog.finxter.com/python-regex-sub/" target="_blank" rel="noreferrer noopener">re.sub</a></strong></h2>
<p>The regex function&nbsp;<code>re.sub(P, R, S)</code>&nbsp;replaces all occurrences of the pattern&nbsp;<code>P</code>&nbsp;with the replacement&nbsp;<code>R</code>&nbsp;in string&nbsp;<code>S</code>. It returns a new string. For example, if you call&nbsp;<code>re.sub('a', 'b', 'aabb')</code>, the result will be the new string&nbsp;<code>'bbbb'</code>&nbsp;with all characters&nbsp;<code>'a'</code>&nbsp;replaced by&nbsp;<code>'b'</code>.</p>
<p><strong>Approach:&nbsp;</strong>Use the&nbsp;<code>re.sub</code>&nbsp;method to replace all occurrences of space characters in the given string with a comma. Thus, the string will now have commas instead of space characters and you can simply split it using a normal string split method by passing comma as the delimiter. </p>
<p>Silly! Isn’t it? Nevertheless, it works. </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 = "abc xyz lmn pqr"
res = re.sub(r'\s+', ',', text).split(',')
print(res)
# ['abc', 'xyz', 'lmn', 'pqr']</pre>
<h2>Method 5: Using filter</h2>
<p>Python’s built-in&nbsp;<code>filter()</code>&nbsp;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 pass the filtering condition.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4da.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read:&nbsp;</strong><a href="https://blog.finxter.com/python-filter/"><strong>Python filter()</strong></a></p>
<p><strong>Approach:&nbsp;</strong>You can use the&nbsp;<code>filter()</code>&nbsp;method to split the string by space. Feed in <code>None</code> as the first argument and the list of split strings as the second argument into the <code>filter</code> function. The&nbsp;<code>filter()</code>&nbsp;function then iterates through the list and filters out the spaces from the given string and returns only the non-whitespace characters. As the&nbsp;<code>filter()</code>&nbsp;method returns an object, we need to use the&nbsp;<code>list()</code>&nbsp;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 = "abc xyz lmn pqr"
print(list(filter(None, text.split())))
# ['abc', 'xyz', 'lmn', 'pqr']</pre>
<h2>Conclusion</h2>
<p>Hurrah! We have successfully solved the given problem using as many as five different ways. I hope you enjoyed reading this&nbsp;<a href="https://blog.finxter.com/"><strong>article</strong></a>&nbsp;and it helped you in your Python coding journey. Please&nbsp;<a href="https://blog.finxter.com/subscribe/"><strong>subscribe and stay tuned</strong></a>&nbsp;for more interesting articles! </p>
<p>Happy coding! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4da.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Suggested Read: <a href="https://blog.finxter.com/python-regex/" target="_blank" rel="noreferrer noopener">Python Regex Superpower [Full Tutorial]</a></strong></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/...le-spaces/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016