How to Remove a Trailing Newline?

Rate this post

Use str.rstrip() to remove a trailing newline. Given a string s, create a new one without trailing newline character by calling s.rstrip('\n'). If you need the string stored in the original variable, simply assign the result to the original variable.

Here’s an example:

>>> my_string = 'hello world\n'
>>> my_string.rstrip('\n')
'hello world'

This actually removes all trailing newline characters:

>>> my_string = 'hello world\n\n\n\n\n\n'
>>> my_string.rstrip('\n')
'hello world'

If you want to remove all trailing whitespaces, use the str.rstrip() method without an argument like so:

>>> my_string = 'hello world\n    \n\n  \t \n\n\n'
>>> my_string.rstrip()
'hello world'

Let’s have a look at the alternatives in more detail next!

Method 1: Remove Single Trailing Newline Character From String with rstrip()

Problem: Remove a single newline character '\n' from the string.

You can solve this problem by using the right-strip method str.rstrip() as you’ve already seen at the beginning of this article.

The str.strip() method trims whitespaces on the right and returns a new string. If you pass a string character argument str.strip(char), it removes the trailing character passed as an argument.

This is a simple example where we print the string with and without trailing whitespace characters:

my_string = 'hello world\n'

print(my_string)
my_string = my_string.rstrip('\n')
print(my_string)

Here’s the output—first the original string with the trailing newline (highlighted in the code) and second the new one without it:

hello world

hello world

The following video gives a short intro to the string.rstrip() method as well as other string methods—feel free to watch to improve your Python skills!

Python String Methods [Ultimate Guide]

But what if you don’t want to remove only a single trailing newline character '\n' but multiple ones (e.g., '\n\n\n')?

Let’s find out next!

Method 2: Remove Multiple Trailing Newline Characters from a String with string.rstrip()

Problem: Remove multiple newline characters (e.g., '\n\n\n') from the string.

The string.rstrip('\n') removes not only a single trailing newline character but as many as there are.

my_string = 'hello world\n\n\n'

print(my_string)
my_string = my_string.rstrip('\n')
print(my_string)

Here’s the output—first the original string with the trailing newline (highlighted in the code) and second the new one without it:

hello world



hello world

Until now, we’ve seen how to remove one or more trailing newline characters '\n' from a given string.

But what if you want to remove all trailing whitespaces from a string—newline '\n', tabular characters '\t', and empty spaces ' ' alike?

Method 3: Remove All Trailing Whitespaces of a String with str.rstrip()

Problem: Remove all trailing whitespaces from a string—newline '\n', tabular characters '\t', and empty spaces ' ' .

To remove all trailing whitespaces, not only newline characters '\n', you can use the str.rstrip() method without any argument. It automatically creates a new string without trailing whitespaces.

You can then assign this new string to your original variable.

Here’s an example of a string with the mixed trailing whitespace sequence '\n\n\n \t \n ':

my_string = 'hello world\n\n\n   \t  \n  '

print(my_string)
my_string = my_string.rstrip()
print(my_string)

The output shows that all trailing whitespace characters have been removed:

hello world


   	  
  
hello world

Okay, there’s something missing—what if you want to remove only a single (but not multiple) trailing newline characters?

Let’s find out next!

Method 4: Remove Only a Single Trailing Newline Character

Problem: Remove a single trailing newline character if there is one from a string.

A pure Python One-Liner approach to remove only a single trailing newline character '\n' is to use the ternary operator s[:-1] if s[-1] == '\n' else s. Here’s how this breaks down:

  • Check the condition if s[-1] == '\n' to find out whether the string ends in a newline character.
  • If yes, use slicing s[:-1] to create a string with all except the last trailing newline character.
  • If no, simply use the original string unchanged—it doesn’t have a trailing newline character.

Here’s an example on how to use the ternary operator:

>>> my_string = 'hello world\n\n'
>>> my_string = my_string[:-1] if my_string[-1] == '\n' else my_string
>>> my_string
'hello world\n'

You can learn more about the ternary operator in my tutorial video here:

The Python Ternary Operator -- And a Surprising One-Liner Hack

A semantically identical solution without trying to be too smart would be the following code where we print the string before and after removing a single newline character:

my_string = 'hello world\n\n\n'

print(my_string)
print('(end)')

# Remove single trailing newline char
if my_string[-1] == '\n':
    my_string = my_string[:-1]
    
print(my_string)
print('(end)')

The output shows that only a single newline character has been removed:

hello world



(end)
hello world


(end)

You can see that exactly one newline character has been removed.

Summary

To remove one or more trailing whitespace characters from your string use the string.rstrip() method without arguments. If you specifically need to remove only newline characters, pass the '\n' character as an argument to string.rstrip('\n').