Strip Newline in Python | 4 Example Codes (Remove Trailing & Leading Blank Line)


In this Python tutorial, I’ll explain how to remove blank newlines from a string. More precisely, I’m going to show you in four examples how to…

  • strip trailing and leading newlines (Example 1)
  • strip trailing newlines only (Example 2)
  • strip leading newlines only (Example 3)
  • remove blank newlines within the text (Example 4)

So without further ado, let’s get started!


Example 1: Remove Trailing & Leading Newlines from String in Python (strip Function)

Before we can start with the examples, we have to create an example string in Python:

# Create example string
my_string = "\n     \n  \nThis is a test string in Python. \nThis is another line. \nAnd another line...\n   \n    \n"

Let’s have a look at our example string:

# Print example string to Python console


Example String for Removal of Newlines in Python

Figure 1: First Example String with Trailing and Leading Newlines.


As you can see based on the blue color in Figure 1, our example text data contains three lines with characters and three trailing as well as three leading blank lines.

In order to delete both, the trailing and leading newlines, we can apply the strip Python function:

# Remove trailing and leading newlines from string
my_string_updated_all = my_string.strip()

Let’s print our updated character string to the Python console:

# Print updated string


Example String After Removing Trailing & Leading Newline

Figure 2: Remove Trailing AND Leading Newlines.


No blank line left!

So what if we want to strip EITHER trailing OR leading whitespace? That’s what I’m going to show you in Examples 2 and 3.


Example 2: Remove Trailing Newline from String (rstrip Function)

If we want to remove trailing newlines only, we have to use the rstrip function:

# Remove trailing newlines from string
my_string_updated_trailing = my_string.rstrip()

Let’s have a look at the resulting string:

# Print updated string


Example String After Removing Trailing Newline

Figure 3: Remove ONLY TRAILING Newlines.


Looks good: The trailing whitespace was removed, but we retained the leading whitespace.


Example 3: Remove Leading Newline from String (lstrip Function)

By applying the lstrip function, we can also do that the other way around:

# Remove leading newlines from string
my_string_updated_leading = my_string.lstrip()

Let’s have a look again:

# Print updated string


Example String After Removing Leading Newline

Figure 4: Remove ONLY LEADING Newlines.


The blank lines at the beginning where removed, but the newlines at the end of our text where kept.


Example 4: Remove Blank Lines within Text (replace Function)

So far we learned how to remove newlines at the beginning or the end of a string. However, sometimes there might be empty lines within a text.

Consider the following example string:

# Remove blank lines within the text
my_string_2 = "This is another example string with blank lines in between. \n\n\nThis is the second line with text."

Let’s see how the second example string looks like:

# Print second example string to Python console


Second Example String in Python

Figure 5: Second Example String with Empty Newlines Between the Text.

In contrast to the first string of Examples 1-3, the second string has blank newlines within the text.

If we want to remove these lines, we can use the replace function in Python:


# Remove newlines between text
my_string_2_updated = my_string_2.replace("\n", "")
# Print updated string


Removing Blank Lines Between Python Text With Replace Function

Figure 6: Remove Newlines Between Text.


Perfect – No blank line anymore!


Video: Working with Textual Data in Python (More Tricks)

Since you are reading this tutorial, I assume that you are working a lot with strings and text data. In case my assumption is correct, I can recommend the following YouTube video tutorial of Corey Schafer. In the video, he is explaining step by step how to deal with textual data. Perfect for beginners!


Further Reading


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.