Python | Split String by Newline

5/5 - (3 votes)

Summary: Use given_string.splitlines() to split a given string by newline.

Minimal Example:

text = 'Python\nJava\nC#'
print(text.splitlines())
# Output: ['Python', 'Java', 'C#']

Problem Formulation

πŸ“œProblem: Given a string, How will you split the string into a list of words using newline as a separator/delimiter?

Example:

# Input
text = """abc
def
ghi
"""
# Expected Output
['abc', 'def', 'ghi']

Let’s dive into the different ways of solving the given problem.

Method 1: Using splitlines

Approach: The easiest way to split a given string by newline is to use the splitlines() method, i.e., simply use – 'given_string'.splitlines().

NOTE: splitlines() is a built-in method in Python that splits a string at line breaks such as '\n' and returns a split list of substrings (i.e., lines). For example, 'finxter\nis\ncool'.splitlines() will return the following list: ['finxter', 'is', 'cool'].

Code:

# Input
text = """abc
def
ghi
"""

print(text.splitlines())

# Output: ['abc', 'def', 'ghi']

🌎Related Read: Python String splitlines()

Method 2: Using split()

Approach: Use 'given_string'.split() to split the given string by a new line character.

Since the split method by default splits a given string by any whitespace character, hence this method will work on our problem as newline(“\n”) is also a whitespace character.

Code:

# Input
text = """abc
def
ghi
"""

print(text.split())

# Output: ['abc', 'def', 'ghi']

⚠️Caution: If you have spaces or tabs in the given string then this method will fail. In case, you try to use s.split('\n') to mitigate the problem, you will still get an extra empty string if there’s a new line character at the end of the given string. Here’s an example that shows when not to use split while splitting a given string by newline.

# Input
text = """abc lmn yzx
def opq  zxy
ghi rst  xyz"""

print(text.split())

# Output: ['abc', 'lmn', 'yzx', 'def', 'opq', 'zxy', 'ghi', 'rst', 'xyz']

# Input
text = """abc
def
ghi
"""

print(text.split('\n'))
# Output: ['abc', 'def', 'ghi', '']

🌎Related Read: Python String split()

Method 3: Using regex

Another way to solve the given problem is to use the split method of the regex module. Since the given string has a new line at the end, hence the last item returned by the re.split method will be an empty string. This means the resultant list will look something like this – ['abc', 'def', 'ghi', '']. To eliminate the extra empty item in the list you can use a list comprehension and specify an if condition to eliminate the occurence of the empty item in the string.

Code:

import re

text = """abc
def
ghi
"""

print([x for x in re.split("\n", text) if x!=''])

# Output: ['abc', 'def', 'ghi']

NOTE: 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

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.

Conclusion

We have successfully solved the given problem using different approaches. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!

Related Reads:
β¦Ώ Python | Split String by Whitespace
β¦Ώ
 How To Cut A String In Python?
β¦Ώ Python | Split String into Characters


Python Regex Course

Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.Β Β 

Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages.Β 

Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.Β Β Regular expressions ​rule the game ​when text processing ​meets computer science.Β 

If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet: