- Python Split String by New Line
- Examples
- 1. Split given string by new line using str.split()
- 2. Split given string by new line using re.split()
- 3. Split given string by one or more newline characters
- Summary
- Python split string new line
- # Table of Contents
- # Split a string on newline characters in Python
- # Split a string on newline characters using str.split()
- # Str.splitlines() vs Str.split()
- # Split a string on newline characters using re.split()
- # Split text by empty line in Python
- # Additional Resources
- Python – Split String by Newline Character
- Split string by Newline
- Fix the number of splits
- Author
Python Split String by New Line
You can split a string in Python with new line as delimiter in many ways.
In this tutorial, we will learn how to split a string by new line character \n in Python using str.split() and re.split() methods.
Examples
1. Split given string by new line using str.split()
In this example, we will take a multiline string string1. We will call the split() method on this string with new line character \n passed as argument. split() method splits the string by new line character and returns a list of strings.
Python Program
string1 = '''Welcome to pythonexamples.org''' #split string by single new-line chunks = string1.split('\n') print(chunks)
['Welcome', 'to', 'PythonExamples.org']
The string can also contain \n characters in the string as shown below, instead of a multi-line string with triple quotes.
Python Program
str = 'Welcome\nto\nPythonExamples.org' #split string by single new-line chunks = str.split('\n') print(chunks)
['Welcome', 'to', 'PythonExamples.org']
2. Split given string by new line using re.split()
In this example, we will split the string by new line using split() method of regular expression re package.
To use re package, we have to import it at the start of our program.
Python Program
import re string1 = '''Welcome to pythonexamples.org''' #split string by single new-line chunks = re.split('\n', string1) print(chunks)
['Welcome', 'to', 'pythonexamples.org']
3. Split given string by one or more newline characters
In this example, we will take a string with substrings separated by one or more new line characters. We will use re package to split the string with one or more new line characters as delimiter. The regular expression that represents one or more new line characters is \n+. We shall pass this expression and the string as arguments to re.split() method.
The syntax of re.split() method is re.split(regular_expression, string). The function returns list of substrings split from string based on the regular_expression.
Python Program
import re str = '''Welcome to PythonExamples.org''' #split string by one or more new-line chunks = re.split('\n+', str) print(chunks)
Regular Expression \n+ represents one or more adjacent new lines. So, one or more new lines is considered as a separator between splits.
['Welcome', 'to', 'PythonExamples.org']
Summary
In this tutorial of Python Examples, we learned how to split a string by new line using String.split() and re.split() methods.
Python split string new line
Last updated: Feb 19, 2023
Reading time · 5 min
# Table of Contents
# Split a string on newline characters in Python
Use the str.splitlines() method to split a string on newline characters, e.g. my_str.splitlines() .
The splitlines method splits the string on each newline character and returns a list of the lines in the string.
Copied!my_str = 'bobby\nhadz\ncom\n' my_list = my_str.splitlines() # 👇️ ['bobby', 'hadz', 'com'] print(my_list)
The str.splitlines method splits the string on newline characters and returns a list containing the lines in the string.
The method doesn’t include the line breaks unless the keepends argument is set to True .
Copied!my_str = 'bobby\nhadz\ncom\n' my_list = my_str.splitlines() # 👇️ ['bobby', 'hadz', 'com'] print(my_list) # 👇️ ['bobby\n', 'hadz\n', 'com\n'] print(my_str.splitlines(True))
The str.splitlines method splits on various line boundaries, e.g. \n , \r , \r\n , etc.
Copied!my_str = 'bobby\rhadz\r\ncom\n' my_list = my_str.splitlines() # 👇️ ['bobby', 'hadz', 'com'] print(my_list)
The \r\n character is a newline character on Windows.
The \n character is a newline character on Linux and macOS.
The \r character is used on old, pre-OS X Macs.
# Split a string on newline characters using str.split()
The str.split() method splits on spaces, tabs and newline characters.
Copied!my_str = 'bobby\rhadz\r\ncom\n' my_list = my_str.split() print(my_list) # 👉️ ['bobby', 'hadz', 'com']
The str.split() method splits the string into a list of substrings using a delimiter.
When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters (including newlines).
Note that the split() method splits on spaces, tabs and newline characters, not just newlines.
Copied!my_str = 'bobby\rhadz\r\ncom\n a b c' my_list = my_str.split() # 👇️ ['bobby', 'hadz', 'com', 'a', 'b', 'c'] print(my_list)
There are some other differences between splitlines() and split() as well.
# Str.splitlines() vs Str.split()
The str.splitlines() method splits on the newline characters for all operating systems and is especially useful when you have to split by \r\n (Windows-style).
If the string ends with a newline character, the splitlines() method removes it, as opposed to the str.split() method.
Copied!my_str = 'bobby\nhadz\ncom\n' my_list = my_str.splitlines() # 👇️ ['bobby', 'hadz', 'com'] print(my_list) # 👇️ ['bobby', 'hadz', 'com', ''] print(my_str.split('\n'))
If there is a whitespace between the text and the newline characters, use the str.strip() method to remove it.
Copied!my_str = 'bobby \nhadz \ncom \n' my_list = [line.strip() for line in my_str.splitlines()] # 👇️ ['bobby', 'hadz', 'com'] print(my_list)
We used a list comprehension to iterate over the list.
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
On each iteration, we call the str.strip() method to strip any leading and trailing whitespace from the string.
If you get empty strings in the result and want to filter them out, use the filter() function.
Copied!my_str = '\rfirst\r\nsecond\nthird\n' my_list = list(filter(None, my_str.splitlines())) # 👇️ ['first', 'second', 'third'] print(my_list)
The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.
Note that the filter() function returns a filter object (not a list). If you need to convert the object to a list, pass it to the list() class.
# Split a string on newline characters using re.split()
You can also use the re.split() method to split a string on the newline characters.
Copied!import re my_str = 'bobby\nhadz\ncom\n' a_list = re.split('\n', my_str) # 👇️ ['bobby', 'hadz', 'com', ''] print(a_list)
If you need to split by one or more newline characters, pass a regular expression to the re.split() method.
Copied!import re my_str = 'bobby\n\n\nhadz\n\ncom\n' a_list = re.split('\n+', my_str) # 👇️ ['bobby', 'hadz', 'com', ''] print(a_list)
The plus + causes the regular expression to match 1 or more repetitions of the preceding character (the newline character).
If you get empty strings in the result and want to filter them out, use the filter() function.
Copied!import re my_str = 'bobby\nhadz\ncom\n' a_list = list(filter(None, re.split('\n', my_str))) # 👇️ ['bobby', 'hadz', 'com'] print(a_list)
The re.split() method takes a regular expression and a string as parameters and splits the string on each occurrence of the regular expression.
# Split text by empty line in Python
You can also use the str.split() method to split text by empty line.
Copied!my_str = """First line Second line Third line""" # 👇️ shows newline characters print(repr(my_str)) # 👉️ 'First line\n\nSecond line\n\nThird line' # 👇️ POSIX style print(my_str.split('\n\n')) # 👉️ ['First line', 'Second line', 'Third line'] # 👇️ Windows style print(my_str.split('\r\n\r\n'))
You can use the repr() function to print the newline characters in your string.
The newline characters should be:
- \n for POSIX style encoded files
- \r\n for Windows-style encoded files
- \r for old Mac encoded files
Once you see what newline characters your text uses, split by 2 of them.
Copied!my_str = """First line Second line Third line""" # 👇️ shows newline characters print(repr(my_str)) # 👉️ 'First line\n\nSecond line\n\nThird line' my_list = my_str.split('\n\n') print(my_list) # 👉️ ['First line', 'Second line', 'Third line'] for line in my_list: print(line)
We have to split by 2 newline characters because the first newline character is from the previous, non-empty line, and the second is from the empty line.
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
If the separator is not found in the string, a list containing only 1 element is returned.
If you get a list with a single element after calling split, then the separator you are using isn’t found in the string.
Copied!my_str = """First line Second line Third line""" # 👇️ shows newline characters print(repr(my_str)) # 👉️ 'First line\n\nSecond line\n\nThird line' my_list = my_str.split('\r\n\r\n') print(my_list) # 👉️ ['First line\n\nSecond line\n\nThird line']
Use the repr() function, like in the example above, to print the newline characters in your string and make sure to split using the correct newline character.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
Python – Split String by Newline Character
In this tutorial, we will look at how to split a string into a list of strings on the occurrences of a newline character, «\n» in Python with the help of examples.
If you prefer a video tutorial over text, check out the following video detailing the steps in this tutorial –
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
Introductory ⭐
Intermediate ⭐⭐⭐
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
You can use the Python string split() function to split a string (by a delimiter) into a list of strings. To split a string by newline character in Python, pass the newline character «\n» as a delimiter to the split() function.
The following is the syntax –
# split string s by newline character s.split("\n")
It returns a list of strings resulting from splitting the original string on the occurrences of a newline, «\n» .
Let’s look at some examples.
Split string by Newline
Here, we pass a newline character, «\n» as the delimiter to the string split() function.
# string with newline characters s = "You are\nMy Fire\nThe one\nDesire" # split string by newline ls = s.split("\n") print(ls)
['You are', 'My Fire', 'The one', 'Desire']
The resulting list contains words resulting from the split of the original string on occurrences of a newline character.
Fix the number of splits
You can also specify the maximum number of splits to be made using the maxsplit parameter. By default, the string split() function makes all the possible splits.
Let’s only split the above string into two parts at the occurrence of a newline character, «\n» starting from the left. To split the string into two parts, the maxsplit should be 1 , because we’re making only one split resulting in two strings.
# string with newline characters s = "You are\nMy Fire\nThe one\nDesire" # split string by newline ls = s.split("\n", maxsplit=1) print(ls)
['You are', 'My Fire\nThe one\nDesire']
You can see that the resulting list has only two strings.
Let’s look at another example.
Let’s split the original string into three parts, here we pass maxsplit=2 .
# string with newline characters s = "You are\nMy Fire\nThe one\nDesire" # split string by newline ls = s.split("\n", maxsplit=2) print(ls)
['You are', 'My Fire', 'The one\nDesire']
The resulting list has only three strings.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Author
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts
Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.