Sick Gaming
[Tut] Python | Split String by Number - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Python | Split String by Number (/thread-100344.html)



[Tut] Python | Split String by Number - xSicKxBot - 12-05-2022

Python | Split String by Number

<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;950806&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:#cbb4f8"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2728.png" alt="✨" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Summary:</strong> To split a string by a number, use the regex <code>split</code> method using the “<code>\d</code>” pattern. </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="">my_string = "#@1abc3$!*5xyz" # Method 1
import re res = re.split('\d+', my_string)
print(res) # Method 2
import re res = re.findall('\D+', my_string)
print(res) # Method 3
from itertools import groupby li = [''.join(g) for _, g in groupby(my_string, str.isdigit)]
res = [x for x in li if x.isdigit() == False]
print(res) # Method 4
res = []
for i in my_string: if i.isdigit() == True: my_string = my_string.replace(i, ",")
print(my_string.split(",")) # Outputs:
# ['#@', 'abc', '$!*', 'xyz']</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 containing different characters. How will you split the string whenever a number appears?</p>
<h2>Method 1: re.split()</h2>
<p>The <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex-split/" target="_blank">re.split(pattern, string)</a></code> method matches all occurrences of the <code>pattern</code> in the <code>string</code> and divides the string along the matches resulting in a list of strings <em>between </em>the matches. For example, <code>re.split('a', 'bbabbbab')</code> results in the list of strings <code>['bb', 'bbb', 'b']</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
my_string = "#@1abc3$!*5xyz"
res = re.split('\d+', my_string)
print(res) # ['#@', 'abc', '$!*', 'xyz']</pre>
<p><strong>Explanation: </strong>The <code>\d</code> <a rel="noreferrer noopener" href="https://blog.finxter.com/regex-special-characters-examples-in-python-re/" target="_blank">special character</a> matches any digit between 0 and 9. By using the maximal number of digits as a delimiter, you split along the digit-word boundary. </p>
<h2>Method 2: re.findall()</h2>
<p>The <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-re-findall/" target="_blank">re.findall(pattern, string)</a></code> method scans <code>string</code> from <strong>left to right</strong>, searching for all <strong>non-overlapping matches</strong> of the <code>pattern</code>. It returns a <strong>list of strings</strong> in the matching order when scanning the string from left to right.</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
my_string = "#@1abc3$!*5xyz"
res = re.findall('\D+', my_string)
print(res) # ['#@', 'abc', '$!*', 'xyz']</pre>
<p><strong>Explanation: </strong>The <code>\</code>D <a rel="noreferrer noopener" href="https://blog.finxter.com/regex-special-characters-examples-in-python-re/" target="_blank">special character</a> matches all characters except any digit between 0 and 9. Thus, you are essentially finding all character groups that appear before the occurrence of a digit. </p>
<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>
<h2>Method 3: itertools.groupby()</h2>
<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="">from itertools import groupby
my_string = "#@1abc3$!*5xyz"
li = [''.join(g) for _, g in groupby(my_string, str.isdigit)]
res = [x for x in li if x.isdigit() == False]
print(res) # ['#@', 'abc', '$!*', 'xyz']</pre>
<p><strong>Explanation:</strong> </p>
<ul>
<li>The <code>itertools.groupby(iterable, key=None)</code> function creates an iterator that returns tuples <code>(key, group-iterator)</code> grouped by each value of <code>key</code>. We use the <code>str.isdigit()</code> function as key function.</li>
<li>The <code>str.isdigit()</code> function returns <code>True</code> if the string consists only of numeric characters. Thus, you will have a list created by using numbers as separators. Note that this list will also contain the numbers as items within it.</li>
<li>In order to eliminate the numbers, use another list comprehension that checks if an element in the list returned previously is a digit or not with the help of the <code>isdigit</code> method. If it is a digit, the item will be discarded. Otherwise it will be stored in the list. </li>
</ul>
<h2>Method 4: Replace Using a for Loop</h2>
<p><strong>Approach: </strong>Use a for loop to iterate through the characters of the given string. Check if a character is a digit or not. As soon as a digit is found, replace that character/digit with a delimiter string ( we have used a comma here) with the help of the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-replace/" target="_blank">replace()</a> method. This basically means that you are placing a particular character in the string whenever a number appears. Once all the digits are replaced by the separator string, split the string by passing the separator string as a delimiter to the split method. </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="">my_string = "#@1abc3$!*5xyz"
res = []
for i in my_string: if i.isdigit(): my_string = my_string.replace(i, ",")
print(my_string.split(",")) # ['#@', 'abc', '$!*', 'xyz']</pre>
<h2><strong>Conclusion</strong></h2>
<p>Phew! We have successfully solved the given problem and managed to do so using four different ways. I hope you found this article helpful and it answered your queries. Please <strong><a href="https://blog.finxter.com/subscribe/">subscribe</a></strong> and stay tuned for more solutions and tutorials. </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/1f310.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read:<a href="https://blog.finxter.com/how-to-split-a-string-between-numbers-and-letters/" target="_blank" rel="noreferrer noopener"> How to Split a String Between Numbers and Letters?</a></strong></p>
</div>


https://www.sickgaming.net/blog/2022/12/03/python-split-string-by-number/