Summary: Use 'given_string'.splitlines(True)
to split the string and also keep the new line character.
Minimal Example:
text = 'abc\nlmn\nxyz' print(text.splitlines(True)) # OUTPUT: ['abc\n', 'lmn\n', 'xyz']
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’s have a look at a test case to understand the given problem.
# 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.
Method 1: Use splitlines(True)
Approach: The splitlines()
method is used to split the string at all line breaks. If you pass True
as a parameter within the splitlines method, then the resultant list includes the newline character along with the substring/item.
Code:
text = """Sun Earth Moon""" print(text.splitlines(True)) # OUTPUT: ['Sun\n', 'Earth\n', 'Moon']
🌎Related Tutorial: Python String splitlines()
Method 2: Use regex
The re.split(pattern, string)
method matches all occurrences of the pattern
in the string
and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab')
results in the list of strings ['bb', 'bbb', 'b']
. Read more here – Python Regex Split.
Approach: Use re.split('(\W)', 'given_string')
where the brackets()
ensure the separators/delimiters are also stored in the list along with the word characters and \W
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.
Code:
import re text = """Sun Earth Moon""" print(re.split('(\W)', text)) # OUTPUT: ['Sun', '\n', 'Earth', '\n', 'Moon']
Note: Instead of “\W
” 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.
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:-
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 split()
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.
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 –
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']
Conclusion
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 Newline
⦿ 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).