{"id":129605,"date":"2022-11-10T00:48:53","date_gmt":"2022-11-10T00:48:53","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=880684"},"modified":"2022-11-10T00:48:53","modified_gmt":"2022-11-10T00:48:53","slug":"python-split-string-and-keep-newline","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/11\/10\/python-split-string-and-keep-newline\/","title":{"rendered":"Python | Split String and Keep Newline"},"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;880684&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;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:#c1f8f8\"><strong>Summary:<\/strong> Use <code>'given_string'.splitlines(True)<\/code> to split the string and also keep the new line character.<\/p>\n<p><strong>Minimal Example<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">text = 'abc\\nlmn\\nxyz'\nprint(text.splitlines(True)) # OUTPUT: ['abc\\n', 'lmn\\n', 'xyz']<\/pre>\n<h2>Problem Formulation<\/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 into a list of substrings and keep the new line character intact?<\/p>\n<p><strong>Example: <\/strong>Let&#8217;s have a look at a test case to understand the given problem.<\/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=\"\"># Input\ntext = \"\"\"Sun\nEarth\nMoon\"\"\" # Expected Output:\n['Sun\\n', 'Earth\\n', 'Moon']\nOR\n['Sun', '\\n', 'Earth', '\\n', 'Moon']<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>Without further ado, let us now dive into the different solutions for the given problem. <\/p>\n<h2><strong>Method 1: Use splitlines<\/strong>(True)<\/h2>\n<p class=\"has-background\" style=\"background-color:#f7e7ce\"><strong>Approach: <\/strong>The&nbsp;<code>splitlines()<\/code> method is used to split the string at all line breaks. If you pass <code>True<\/code> as a parameter within the splitlines method, then the resultant list includes the newline character along with the substring\/item. <\/p>\n<p><strong>Code: <\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">text = \"\"\"Sun\nEarth\nMoon\"\"\"\nprint(text.splitlines(True)) # OUTPUT: ['Sun\\n', 'Earth\\n', 'Moon']<\/pre>\n<p class=\"has-base-2-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30e.png\" alt=\"\ud83c\udf0e\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Related Tutorial: <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-string-splitlines\/\" target=\"_blank\">Python String splitlines()<\/a><\/strong><\/p>\n<h2><strong>Method 2: Use <a href=\"https:\/\/blog.finxter.com\/python-regex\/\" target=\"_blank\" rel=\"noreferrer noopener\">regex<\/a><\/strong><\/h2>\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>. Read more here \u2013&nbsp;<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-regex-split\/\" target=\"_blank\">Python Regex Split<\/a><\/strong>.<\/p>\n<p class=\"has-background\" style=\"background-color:#f7e7ce\"><strong>Approach: <\/strong>Use <code>re.split('(\\W)', 'given_string')<\/code> where the brackets<code>()<\/code>&nbsp;ensure the separators\/delimiters are also stored in the list along with the word characters and <code>\\W<\/code>&nbsp;is a special sequence that returns a match where it does not find any word characters in the given string. Here it is used to find the delimiters while splitting the string.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"5\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re\ntext = \"\"\"Sun\nEarth\nMoon\"\"\"\nprint(re.split('(\\W)', text)) # OUTPUT: ['Sun', '\\n', 'Earth', '\\n', 'Moon']<\/pre>\n<p><strong>Note: <\/strong>Instead of &#8220;<code>\\W<\/code>&#8221; you are free to use any other expression that suits your needs however, make sure to enclose it within brackets to ensure that the newline characters (delimiter) are also included. <\/p>\n<p>In case you do not want to include the separators as independent items, instead, you want to include them along with the split substrings\/items, then you can simply split the given string using &#8220;\\n&#8221; as the separator and then append or concatenate the newline character to each substring\/item one by one except the last item. This is what you can do:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"5-9,13,14\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re\ntext = \"\"\"Sun\nEarth\nMoon\"\"\"\nres = re.split('\\n', text)\noutput = []\nfor i in range(len(res)-1): output.append(res[i]+\"\\n\")\noutput.append(res[-1])\nprint(output) # Alternate Formulation\nres = [x+\"\\n\" for x in re.split('\\n', text)]\nres[-1] = res[-1].strip('\\n')\nprint(res) # OUTPUT: ['Sun\\n', 'Earth\\n', 'Moon']<\/pre>\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: Using a&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a>&nbsp;<\/h2>\n<p class=\"has-background\" style=\"background-color:#f7e7ce\"><strong>Approach: <\/strong>Use a list comprehension to split the given string using a for loop and the <code>split()<\/code> method and return each substring as an item and concatenate the separator (&#8220;new line character&#8221; in this case) along with the item. Note that the resultant list will have an extra &#8220;\\n&#8221; character at the end. You can simply strip this new line character from the last element of the list.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"5,7\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">text = \"\"\"Sun\nEarth\nMoon\"\"\"\n# split string and keep \"\\n\"\nres = [x+\"\\n\" for x in text.split()]\n# remove the extra \"\\n\" character from the last item of the list res[-1] = res[-1].strip('\\n')\nprint(res) # OUTPUT: ['Sun\\n', 'Earth\\n', 'Moon']<\/pre>\n<p>If you want the separator as an independent item in the list then go for the following expression &#8211;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4,5\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">text = \"\"\"Sun\nEarth\nMoon\"\"\"\nres = [u for x in text.split('\\n') for u in (x, '\\n')]\nres.pop(-1)\nprint(res) # OUTPUT: ['Sun', '\\n', 'Earth', '\\n', 'Moon']<\/pre>\n<h2>Conclusion<\/h2>\n<p>We have successfully solved the given problem using different approaches. I hope this\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\">article<\/a>\u00a0helped you in your Python coding journey. Please\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/subscribe\" target=\"_blank\">subscribe and stay tuned<\/a>\u00a0for more interesting articles. <\/p>\n<p>Happy Pythoning! <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;\" \/>\u00a0<\/p>\n<p class=\"has-base-background-color has-background\"><strong>Related Reads:<br \/><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-split-string-by-newline\/\" target=\"_blank\">\u29bf Python | Split String by Newline<\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-cut-a-string-in-python\/\" target=\"_blank\"><br \/><\/a><a href=\"https:\/\/blog.finxter.com\/how-to-split-a-string-and-keep-the-separators\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u29bf&nbsp;How To Split A String And Keep The Separators?<\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-split-string-into-characters\/\" target=\"_blank\"><br \/>\u29bf&nbsp;Python | Split String into Characters<\/a><\/strong><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p><strong>But before we move on, I&#8217;m excited to present you my new Python book <a rel=\"noreferrer noopener\" href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" title=\"https:\/\/amzn.to\/2WAYeJE\">Python One-Liners<\/a><\/strong> (Amazon Link).<\/p>\n<p>If you like one-liners, you&#8217;ll LOVE the book. It&#8217;ll teach you everything there is to know about a <strong>single line of Python code.<\/strong> But it&#8217;s also an <strong>introduction to computer science<\/strong>, data science, machine learning, and algorithms. <strong><em>The universe in a single line of Python!<\/em><\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" width=\"215\" height=\"283\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/02\/image-1.png\" alt=\"\" class=\"wp-image-5969\"\/><\/a><\/figure>\n<\/div>\n<p>The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). <\/p>\n<p>Link: <a href=\"https:\/\/nostarch.com\/pythononeliners\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/nostarch.com\/pythononeliners<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post Summary: Use &#8216;given_string&#8217;.splitlines(True) to split the string and also keep the new line character. Minimal Example: text = &#8216;abc\\nlmn\\nxyz&#8217; print(text.splitlines(True)) # OUTPUT: [&#8216;abc\\n&#8217;, &#8216;lmn\\n&#8217;, &#8216;xyz&#8217;] Problem Formulation Problem: Given a string. How will you split the string into a list of substrings and keep the new line character intact? Example: Let&#8217;s have [&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-129605","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\/129605","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=129605"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129605\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=129605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=129605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=129605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}