![]() |
|
[Tut] Python | Split String and Keep Newline - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://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 Rate this post Summary: Use Code: 'given_string'.splitlines(True)Minimal Example: Code: text = 'abc\nlmn\nxyz'
print(text.splitlines(True)) # OUTPUT: ['abc\n', 'lmn\n', 'xyz'] Problem: Given a string. How will you split the string into a list of substrings and keep the new line character intact?Example: Let’s have a look at a test case to understand the given problem. Code: # Input
text = """Sun
Earth
Moon""" # Expected Output:
['Sun\n', 'Earth\n', 'Moon']
OR
['Sun', '\n', 'Earth', '\n', 'Moon']Without further ado, let us now dive into the different solutions for the given problem. [b]Method 1: Use splitlines(True)[/b] Approach: The Code: splitlines()Code: TrueCode: Code: text = """Sun
Earth
Moon"""
print(text.splitlines(True)) # OUTPUT: ['Sun\n', 'Earth\n', 'Moon'] Related Tutorial: Python String splitlines()[b]Method 2: Use regex[/b] The Code: re.split(pattern, string)Code: patternCode: stringCode: re.split('a', 'bbabbbab')Code: ['bb', 'bbb', 'b']Approach: Use Code: re.split('(\W)', 'given_string')Code: ()Code: \WCode: Code: import re
text = """Sun
Earth
Moon"""
print(re.split('(\W)', text)) # OUTPUT: ['Sun', '\n', 'Earth', '\n', 'Moon']Note: Instead of “ Code: \WIn 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:- Code: 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']Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python 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. Method 3: Using a List Comprehension Approach: Use a list comprehension to split the given string using a for loop and the Code: split()Code: Code: 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']If you want the separator as an independent item in the list then go for the following expression – Code: 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']We have successfully solved the given problem using different approaches. I hope this article helped you in your Python coding journey. Please subscribe and stay tuned for more interesting articles. Happy Pythoning! Related Reads: ⦿ Python | Split String by Newlinehttps://blog.finxter.com/how-to-cut-a-string-in-python/⦿ How To Split A String And Keep The Separators?⦿ Python | Split String into Characters But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link). If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python! ![]() The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). Link: https://nostarch.com/pythononeliners |