Split a string on newline characters in Python

avatar

Borislav Hadzhiev

Last updated: Jun 23, 2022

banner

Photo from Unsplash

Split a string on newline characters in Python #

Use the str.splitlines() method to split a string on newline characters, e.g. my_list = my_str.splitlines(). The splitlines method splits the string on each newline character and returns a list of the lines in the string.

main.py
my_str = 'first\nsecond\nthird\n' my_list = my_str.splitlines() # 👇️ ['first', 'second', 'third'] 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 does not include the line breaks unless the keepends argument is set to True.

main.py
my_str = 'first\nsecond\nthird\n' my_list = my_str.splitlines() # 👇️ ['first', 'second', 'third'] print(my_list) # 👇️ ['first\n', 'second\n', 'third\n'] print(my_str.splitlines(True))

The str.splitlines method splits on various line boundaries, e.g. \n, \r, \r\n, etc.

main.py
my_str = 'first\rsecond\r\nthird\n' my_list = my_str.splitlines() # 👇️ ['first', 'second', 'third'] print(my_list)

If the string ends with a newline character, the splitlines() method removes it, as opposed to the str.split() method.

main.py
my_str = 'first\nsecond\nthird\n' my_list = my_str.splitlines() # 👇️ ['first', 'second', 'third'] print(my_list) # 👇️ ['first', 'second', 'third', ''] print(my_str.split('\n'))

If there is whitespace between the text and the newline characters, use the str.strip() method to remove it.

main.py
my_str = 'first \nsecond \nthird \n' my_list = [line.strip() for line in my_str.splitlines()] # 👇️ ['first', 'second', 'third'] 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.

main.py
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.

If you pass None for the function argument, all falsy elements of the iterable are removed.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.