Read multiline string line by line in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Read multiline string line by line in Python #

Use the str.splitlines() method to read a multiline string line by line, e.g. for line in multiline_str.splitlines():. The str.splitlines() method splits the string on each newline character and returns a list of the lines in the string.

main.py
multiline_str = """\ First line Second line Third line""" # First line # Second line # Third line for line in multiline_str.splitlines(): print(line)

The str.splitlines method splits the string on newline characters and returns a list containing the lines in the string.

main.py
multiline_str = """\ First line Second line Third line""" lines = multiline_str.splitlines() print(lines) # 👉️ ['First line', 'Second line', 'Third line']

The method doesn't include the line breaks unless the keepends argument is set to True.

main.py
multiline_str = """\ First line Second line Third line""" lines = multiline_str.splitlines(True) print(lines) # 👉️ ['First line\n', 'Second line\n', 'Third line']

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

main.py
my_str = "one\rtwo\r\nthree\n" lines = my_str.splitlines() print(lines) # 👉️ ['one', 'two', 'three']

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

main.py
my_str = "one\ntwo\nthree\n" lines = my_str.splitlines() print(lines) # 👉️ ['one', 'two', 'three'] lines = my_str.split('\n') print(lines) # 👉️ ['one', 'two', 'three', '']

Notice that the last item in the list the str.split() method returned is an empty string.

Once you use the str.splitlines() to get a list of the lines in the string, use a for loop to iterate over the list and read it line by line.
main.py
multiline_str = """\ First line Second line Third line""" # First line # Second line # Third line for line in multiline_str.splitlines(): print(line)

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

main.py
my_str = 'one \ntwo \n three \n' # 👇️ ['one ', 'two ', ' three '] print(my_str.splitlines()) lines = [line.strip() for line in my_str.splitlines()] print(lines) # 👉️ ['one', 'two', 'three']

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 = '\rone\r\ntwo\nthree\n' # 👇️ ['', 'one', 'two', 'three'] print(my_str.splitlines()) lines = list(filter(None, my_str.splitlines())) # one # two # three for line in lines: print(line)

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.

Empty strings are falsy values, so the empty string list item got removed from the list.

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.