{"id":130239,"date":"2022-12-03T18:05:33","date_gmt":"2022-12-03T18:05:33","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=950806"},"modified":"2022-12-03T18:05:33","modified_gmt":"2022-12-03T18:05:33","slug":"python-split-string-by-number","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/12\/03\/python-split-string-by-number\/","title":{"rendered":"Python | Split String by Number"},"content":{"rendered":"\n<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;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> <span class=\"kksr-muted\">Rate this post<\/span> <\/div>\n<\/div>\n<p class=\"has-background\" style=\"background-color:#cbb4f8\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2728.png\" alt=\"\u2728\" 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 &#8220;<code>\\d<\/code>&#8221; pattern. <\/p>\n<h3><strong>Minimal Example<\/strong><\/h3>\n<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\nimport re res = re.split('\\d+', my_string)\nprint(res) # Method 2\nimport re res = re.findall('\\D+', my_string)\nprint(res) # Method 3\nfrom itertools import groupby li = [''.join(g) for _, g in groupby(my_string, str.isdigit)]\nres = [x for x in li if x.isdigit() == False]\nprint(res) # Method 4\nres = []\nfor i in my_string: if i.isdigit() == True: my_string = my_string.replace(i, \",\")\nprint(my_string.split(\",\")) # Outputs:\n# ['#@', 'abc', '$!*', 'xyz']<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2><strong>Problem Formulation<\/strong><\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4dc.png\" alt=\"\ud83d\udcdc\" 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>\n<h2>Method 1: re.split()<\/h2>\n<p>The\u00a0<code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-regex-split\/\" target=\"_blank\">re.split(pattern, string)<\/a><\/code>\u00a0method matches all occurrences of the\u00a0<code>pattern<\/code>\u00a0in the\u00a0<code>string<\/code>\u00a0and divides the string along the matches resulting in a list of strings\u00a0<em>between\u00a0<\/em>the matches. For example,\u00a0<code>re.split('a', 'bbabbbab')<\/code>\u00a0results in the list of strings\u00a0<code>['bb', 'bbb', 'b']<\/code>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<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\nmy_string = \"#@1abc3$!*5xyz\"\nres = re.split('\\d+', my_string)\nprint(res) # ['#@', 'abc', '$!*', 'xyz']<\/pre>\n<p><strong>Explanation: <\/strong>The\u00a0<code>\\d<\/code>\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/regex-special-characters-examples-in-python-re\/\" target=\"_blank\">special character<\/a>\u00a0matches any digit between 0 and 9. By using the maximal number of digits as a delimiter, you split along the digit-word boundary.\u00a0<\/p>\n<h2>Method 2: re.findall()<\/h2>\n<p>The\u00a0<code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-re-findall\/\" target=\"_blank\">re.findall(pattern, string)<\/a><\/code>\u00a0method scans\u00a0<code>string<\/code>\u00a0from\u00a0<strong>left to right<\/strong>, searching for all\u00a0<strong>non-overlapping matches<\/strong>\u00a0of the\u00a0<code>pattern<\/code>. It returns a\u00a0<strong>list of strings<\/strong>\u00a0in the matching order when scanning the string from left to right.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<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\nmy_string = \"#@1abc3$!*5xyz\"\nres = re.findall('\\D+', my_string)\nprint(res) # ['#@', 'abc', '$!*', 'xyz']<\/pre>\n<p><strong>Explanation: <\/strong>The\u00a0<code>\\<\/code>D\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/regex-special-characters-examples-in-python-re\/\" target=\"_blank\">special character<\/a>\u00a0matches 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>\n<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>\n<h2>Method 3: itertools.groupby()<\/h2>\n<p><strong>Code:<\/strong><\/p>\n<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\nmy_string = \"#@1abc3$!*5xyz\"\nli = [''.join(g) for _, g in groupby(my_string, str.isdigit)]\nres = [x for x in li if x.isdigit() == False]\nprint(res) # ['#@', 'abc', '$!*', 'xyz']<\/pre>\n<p><strong>Explanation:<\/strong> <\/p>\n<ul>\n<li>The\u00a0<code>itertools.groupby(iterable, key=None)<\/code>\u00a0function creates an iterator that returns tuples\u00a0<code>(key, group-iterator)<\/code>\u00a0grouped by each value of\u00a0<code>key<\/code>. We use the\u00a0<code>str.isdigit()<\/code>\u00a0function as key function.<\/li>\n<li>The\u00a0<code>str.isdigit()<\/code>\u00a0function returns\u00a0<code>True<\/code>\u00a0if 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>\n<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>\n<\/ul>\n<h2>Method 4: Replace Using a for Loop<\/h2>\n<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>\n<p><strong>Code:<\/strong><\/p>\n<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\"\nres = []\nfor i in my_string: if i.isdigit(): my_string = my_string.replace(i, \",\")\nprint(my_string.split(\",\")) # ['#@', 'abc', '$!*', 'xyz']<\/pre>\n<h2><strong>Conclusion<\/strong><\/h2>\n<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>\n<p>Happy coding! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f310.png\" alt=\"\ud83c\udf10\" 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>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post Summary: To split a string by a number, use the regex split method using the &#8220;\\d&#8221; pattern. Minimal Example my_string = &#8220;#@1abc3$!*5xyz&#8221; # Method 1 import re res = re.split(&#8216;\\d+&#8217;, my_string) print(res) # Method 2 import re res = re.findall(&#8216;\\D+&#8217;, my_string) print(res) # Method 3 from itertools import groupby li = [&#8221;.join(g) [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-130239","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=130239"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130239\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}