{"id":130259,"date":"2022-12-04T16:18:23","date_gmt":"2022-12-04T16:18:23","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=953017"},"modified":"2022-12-04T16:18:23","modified_gmt":"2022-12-04T16:18:23","slug":"python-split-string-multiple-whitespaces","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/12\/04\/python-split-string-multiple-whitespaces\/","title":{"rendered":"Python | Split String Multiple Whitespaces"},"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;953017&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:#99fbfb\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f34e.png\" alt=\"\ud83c\udf4e\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Summary:<\/strong> The most efficient way to split a string using multiple whitespaces 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>\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=\"\">import re text = \"mouse\\nsnake\\teagle human\"\n# Method 1\nprint(text.split()) # Method 2\nres = re.split(\"\\s+\", text)\nprint(res) # Method 3\nres = re.sub(r'\\s+', ',', text).split(',')\nprint(res) # Method 4\nprint(re.findall(r'\\S+', text)) # ['mouse', 'snake', 'eagle', 'human']<\/pre>\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. How will you split the string using multiple whitespaces?<\/p>\n<h3 class=\"has-large-font-size\"><strong>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=\"\"># Input\ntext = \"abc\\nlmn\\tpqr xyz\\rmno\"\n# Output\n['abc', 'lmn', 'pqr', 'xyz', 'mno']<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>There are numerous ways of solving the given problem. So, without further ado, let us dive into the solutions. <\/p>\n<h2>Method 1: Using <a href=\"https:\/\/blog.finxter.com\/python-regex\/\" target=\"_blank\" rel=\"noreferrer noopener\">Regex<\/a><\/h2>\n<p>The best way to deal with multiple delimiters is to use the flexibility of the regular expressions library. There are different functions available in the regex library that you can use to split the given string. Let&#8217;s go through each one by one.<\/p>\n<h3><strong>1.1 Using re.split<\/strong><\/h3>\n<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4da.png\" alt=\"\ud83d\udcda\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Recommended Read: \u00a0<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-regex-split\/\" target=\"_blank\">Python Regex Split<\/a><\/strong>.<\/strong><\/p>\n<p><strong>Approach: <\/strong>To split the string using multiple whitespace characters use <code>re.split(\"\\s+\", text)<\/code> where <code>\\s<\/code>\u00a0is the matching pattern and it represents a special sequence that returns a match whenever it finds any whitespace character and splits the string.<\/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\ntext = \"abc\\nlmn\\tpqr xyz\\rmno\"\nres = re.split(\"\\s+\", text)\nprint(res) # ['abc', 'lmn', 'pqr', 'xyz', 'mno']<\/pre>\n<h3><strong>1.2 Using re.findall<\/strong><\/h3>\n<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>\n<p class=\"has-base-background-color has-background\"><strong><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4da.png\" alt=\"\ud83d\udcda\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>Recommended Read: <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-re-findall\/\" target=\"_blank\">Python re.findall() \u2013 Everything You Need to Know<\/a><\/strong><\/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 text = \"abc\\nlmn\\tpqr xyz\\rmno\"\nprint(re.findall(r'\\S+', text))<\/pre>\n<p><strong>Explanation: <\/strong>In the expression, i.e., <code>re.findall(r\"\\S'+\", text)<\/code>, all occurrences of characters except whitespaces are found and stored in a list. Here, <code>\\S+<\/code> returns a match whenever the string contains one or more occurrences of normal characters (characters from a to Z, digits from 0-9, etc. However, not the whitespaces are considered). <\/p>\n<h3> <strong>1.3 Using re.sub<\/strong><\/h3>\n<p>The regex function\u00a0<code>re.sub(P, R, S)<\/code>\u00a0replaces all occurrences of the pattern\u00a0<code>P<\/code>\u00a0with the replacement\u00a0<code>R<\/code>\u00a0in string\u00a0<code>S<\/code>. It returns a new string. For example, if you call\u00a0<code>re.sub('a', 'b', 'aabb')<\/code>, the result will be the new string\u00a0<code>'bbbb'<\/code>\u00a0with all characters\u00a0<code>'a'<\/code>\u00a0replaced by\u00a0<code>'b'<\/code>.<\/p>\n<p><strong>Aprroach: <\/strong>Use the <code>re.sub<\/code> method to replace all occurrences of whitespace characters in the given string with a comma. Thus, the string will now have commas instead of whitespace characters and you can simply split it using a normal string split method by passing comma as the delimiter.<\/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\ntext = \"abc\\nlmn\\tpqr xyz\\rmno\"\nres = re.sub(r'\\s+', ',', text).split(',')\nprint(res) # ['abc', 'lmn', 'pqr', 'xyz', 'mno']<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\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<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 2: Using <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-string-split\/\" target=\"_blank\">split()<\/a><\/h2>\n<p>By default the <code>split<\/code> 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. You can use this default property of the split function and successfully split the given string at multiple whitespaces just by using the <code>split()<\/code> function.<\/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=\"\">text = \"abc\\nlmn\\tpqr xyz\\rmno\"\nprint(text.split())\n# ['abc', 'lmn', 'pqr', 'xyz', 'mno']<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4da.png\" alt=\"\ud83d\udcda\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Recommended Digest<\/strong>: <strong><a href=\"https:\/\/blog.finxter.com\/python-string-split\/\"> Python String split()<\/a><\/strong> <\/p>\n<h2>Conclusion<\/h2>\n<p>We have successfully solved the given problem using different approaches. Simply using split could do the job for you. However, feel free to explore and try out the other options mentioned above. I hope this\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\"><strong>article<\/strong><\/a>\u00a0helped you in your Python coding journey. Please\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/subscribe\" target=\"_blank\"><strong>subscribe and stay tuned<\/strong><\/a>\u00a0for more interesting articles.<\/p>\n<p>Happy Pythoning!&nbsp;<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f40d.png\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>&nbsp;<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<div class=\"is-layout-flow wp-block-group\">\n<div class=\"wp-block-group__inner-container\">\n<h2><a href=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\">Python Regex Course<\/a><\/h2>\n<p><strong><em>Google engineers are regular expression masters. <\/em><\/strong>The Google search engine is a massive <em>text-processing engine<\/em> that extracts value from trillions of webpages.\u00a0\u00a0<\/p>\n<p><strong><em>Facebook engineers are regular expression masters.<\/em><\/strong> Social networks like Facebook, WhatsApp, and Instagram connect humans via <em>text messages<\/em>.\u00a0<\/p>\n<p><strong><em>Amazon engineers are regular expression masters. <\/em><\/strong>Ecommerce giants ship products based on <em>textual product descriptions<\/em>.\u00a0\u00a0Regular expressions \u200brule the game \u200bwhen text processing \u200bmeets computer science.\u00a0<\/p>\n<p><em><strong>If you want to become a regular expression master too, check out the<a href=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\"> most comprehensive Python regex course<\/a> on the planet:<\/strong><\/em><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-1024x576.jpg\" alt=\"\" class=\"wp-image-19840\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-768x432.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-1536x864.jpg 1536w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-2048x1152.jpg 2048w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-150x84.jpg 150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post Summary: The most efficient way to split a string using multiple whitespaces is to use the split function like so given_string.split(). An alternate approach is to use different functions of the regex package to split the string at multiple whitespaces. Minimal Example: import re text = &#8220;mouse\\nsnake\\teagle human&#8221; # Method 1 print(text.split()) [&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-130259","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\/130259","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=130259"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130259\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}