Sick Gaming
[Tut] Python | Split String and Keep Newline - 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 and Keep Newline (/thread-100211.html)



[Tut] Python | Split String and Keep Newline - xSicKxBot - 11-11-2022

Python | Split String and Keep Newline

<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;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;}'>
<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:#c1f8f8"><strong>Summary:</strong> Use <code>'given_string'.splitlines(True)</code> to split the string and also keep the new line character.</p>
<p><strong>Minimal Example</strong>:</p>
<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'
print(text.splitlines(True)) # OUTPUT: ['abc\n', 'lmn\n', 'xyz']</pre>
<h2>Problem Formulation</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 into a list of substrings and keep the new line character intact?</p>
<p><strong>Example: </strong>Let’s have a look at a test case to understand the given problem.</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=""># Input
text = """Sun
Earth
Moon""" # Expected Output:
['Sun\n', 'Earth\n', 'Moon']
OR
['Sun', '\n', 'Earth', '\n', 'Moon']</pre>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p>Without further ado, let us now dive into the different solutions for the given problem. </p>
<h2><strong>Method 1: Use splitlines</strong>(True)</h2>
<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>
<p><strong>Code: </strong></p>
<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
Earth
Moon"""
print(text.splitlines(True)) # OUTPUT: ['Sun\n', 'Earth\n', 'Moon']</pre>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" 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>
<h2><strong>Method 2: Use <a href="https://blog.finxter.com/python-regex/" target="_blank" rel="noreferrer noopener">regex</a></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>. Read more here –&nbsp;<strong><a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex-split/" target="_blank">Python Regex Split</a></strong>.</p>
<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>
<p><strong>Code:</strong></p>
<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
text = """Sun
Earth
Moon"""
print(re.split('(\W)', text)) # OUTPUT: ['Sun', '\n', 'Earth', '\n', 'Moon']</pre>
<p><strong>Note: </strong>Instead of “<code>\W</code>” 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>
<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 “\n” 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>
<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
text = """Sun
Earth
Moon"""
res = re.split('\n', text)
output = []
for i in range(len(res)-1): output.append(res[i]+"\n")
output.append(res[-1])
print(output) # Alternate Formulation
res = [x+"\n" for x in re.split('\n', text)]
res[-1] = res[-1].strip('\n')
print(res) # OUTPUT: ['Sun\n', 'Earth\n', 'Moon']</pre>
<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: Using a&nbsp;<a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a>&nbsp;</h2>
<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 (“new line character” in this case) along with the item. Note that the resultant list will have an extra “\n” character at the end. You can simply strip this new line character from the last element of the list.</p>
<p><strong>Code:</strong></p>
<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
Earth
Moon"""
# split string and keep "\n"
res = [x+"\n" for x in text.split()]
# remove the extra "\n" character from the last item of the list res[-1] = res[-1].strip('\n')
print(res) # OUTPUT: ['Sun\n', 'Earth\n', 'Moon']</pre>
<p>If you want the separator as an independent item in the list then go for the following expression –</p>
<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
Earth
Moon"""
res = [u for x in text.split('\n') for u in (x, '\n')]
res.pop(-1)
print(res) # OUTPUT: ['Sun', '\n', 'Earth', '\n', 'Moon']</pre>
<h2>Conclusion</h2>
<p>We have successfully solved the given problem using different approaches. I hope this <a rel="noreferrer noopener" href="https://blog.finxter.com/" target="_blank">article</a> helped you in your Python coding journey. Please <a rel="noreferrer noopener" href="https://blog.finxter.com/subscribe" target="_blank">subscribe and stay tuned</a> for more interesting articles. </p>
<p>Happy Pythoning! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f40d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>
<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">⦿ 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">⦿&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 />⦿&nbsp;Python | Split String into Characters</a></strong></p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p><strong>But before we move on, I’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>
<p>If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a <strong>single line of Python code.</strong> But it’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>
<div class="wp-block-image">
<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>
</div>
<p>The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). </p>
<p>Link: <a href="https://nostarch.com/pythononeliners" target="_blank" rel="noreferrer noopener">https://nostarch.com/pythononeliners</a></p>
</div>


https://www.sickgaming.net/blog/2022/11/10/python-split-string-and-keep-newline/