Last updated: Apr 10, 2024
Reading timeยท4 min
\n
To remove the empty lines from a string:
str.splitlines()
method to split the string on newline characters.str.join()
method to join the list with os.linesep
as the
separator.import os multiline_string = """\ First line Second line Third line """ without_empty_lines = os.linesep.join( [ line for line in multiline_string.splitlines() if line ] ) # First line # Second line # Third line print(without_empty_lines)
The str.splitlines
method splits the string on newline characters and returns
a list containing the lines in the string.
multiline_string = """\ First line Second line Third line """ # ๐๏ธ ['First line', '', 'Second line', '', 'Third line', '', ''] print(multiline_string.splitlines())
We used a list comprehension to iterate over the list of lines.
On each iteration, we check if the current line is truthy to exclude empty strings from the result.
The last step is to use the str.join()
method to join the filtered list.
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
The string the method is called on is used as the separator between the elements.
We used os.linesep
as the separator.
The os.linesep attribute returns the string used to separate lines on the current platform.
For example \n
on Unix and \r\n
on Windows.
I'm on Linux, so this is the output of os.linesep
for me.
import os print(repr(os.linesep)) # ๐๏ธ '\n'
str.strip()
method.If you need to remove the empty lines that may or may not contain whitespace:
str.splitlines()
method to split the string on newline characters.str.strip()
method to filter out empty lines that may contain
whitespace.str.join()
method to join the list with a newline character
separator.import os multiline_string = """\ First line Second line Third line """ without_empty_lines = os.linesep.join([ line for line in multiline_string.splitlines() if line.strip() != '' ]) # First line # Second line # Third line print(without_empty_lines)
str.strip()
method to remove the whitespace and compare the result to an empty string.Here is an example of calling str.splitlines()
on a multiline string where
some of the empty lines contain only whitespace characters.
multiline_string = """\ First line Second line Third line """ # ๐๏ธ ['First line', ' ', 'Second line', ' ', 'Third line', ' ', ''] print(multiline_string.splitlines())
The str.strip method returns a copy of the string with the leading and trailing whitespace removed.
If the line is equal to an empty string once the leading and trailing whitespace is removed, we consider it to be an empty line.
Alternatively, you can use the str.join()
method with a newline character to
avoid an extra import.
\n
This is a four-step process:
str.splitlines()
method to split the string on newline characters.str.join()
method to join the filtered list with \n
as the
separator.multiline_string = """\ First line Second line Third line """ without_empty_lines = '\n'.join([ line for line in multiline_string.splitlines() if line ]) # First line # Second line # Third line print(without_empty_lines)
We used the \n
(newline) character as the separator in the example to not have
to import the os
module.
However, note that this approach doesn't handle the scenario where the lines in
the multiline string are separated by another character, e.g. \r\n\
(Windows).
For a solution that is consistent between operating systems, stick to the
os.linesep
attribute.
You can learn more about the related topics by checking out the following tutorials: