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 –
How to split a string in Python?

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"
.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
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)
Output:
['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)
Output:
['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)
Output:
['You are', 'My Fire', 'The one\nDesire']
The resulting list has only three strings.
You might also be interested in –
- Python – Split String by Underscore
- Python – Remove Multiple Spaces From a String
- Remove Linebreaks From String in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.