How to remove a trailing newline in Python?

In this Python tutorial, you will learn how to remove a trailing newline from a string in Python.

Table Of Contents

Let’s dive into the tutorial.

What is trailing newline?

A trailing newline is the character – \n that is placed at the end in the string.

Example:

"Hello this is mystring\n"

There are different ways to remove a trailing newline. Let’s discuss them one by one,

Remove trailing newline characters using rstrip()

The rstrip() method of string class, deletes the newline characters and spaces from the end of the string. We can use this to remove the trailing newline characters in the given string.

Syntax:

input_string.rstrip()

Where input_string is the string from which we need to delete the trailing newline character.

Example:

In this example, we will create a string with a trailing newline character. Then we will remove that trailing newline character from string using the rstrip() method.

input_string = "Welcome to thisPointer\n"

# Display actual string
print("Actual String: ", input_string)

# Remove trailing line using rstrip()
modified = input_string.rstrip()

# Display modified string
print("After removing trailing newline: ", modified)

Output:

Actual String:  Welcome to thisPointer

After removing trailing newline:  Welcome to thisPointer

Remove trailing newline using regex module

The regex module (re) in Python is used for regular expressions. We can utilize this module to remove the trailing newline character from the given input string. The sub() method in regex is used to replace any kind of character with the specified character. Here, we have to replace \n with empty(”).
Syntax:

regex.sub('\n', '', input_string)

Where input_string is the string.

Example:
In this example, we will create a string with trailing newline characters and remove them using regex.sub() method.

import re

input_string = "welcome\n to\n thisPointer\n\n\n"

# display actual string
print("Actual string: ",input_string)

# Remove trailing characters using regex.sub()
input_string = re.sub("(\n+)$", "", input_string)

print("Removed Trailing characters: ", input_string)

Output:

Actual string:  welcome
 to
 thisPointer



Removed Trailing characters:  welcome
 to
 thisPointer

We can see that the trailing newline characters from the string are removed.

It can also be possible to demonstrate this using list.

Example:

import re

input_list = ["welcome\n", "to\n", "thisPointer\n\n\n"] 

print(input_list)

# Create an empty list
final_list = []

# use re.sub() to replace trailing 
# newline characters with empty strings
for iterator in input_list:
    final_list.append(re.sub("(\n+)$", "", iterator))

print(final_list)

Output:

['welcome\n', 'to\n', 'thisPointer\n\n\n ']
['welcome', 'to', 'thisPointer ']

Remove newline characters using replace() function

The replace() method of string class can also be used to replace any kind of character with the specified character in string. Here, we have to replace \n with empty(“”).

Syntax:

input_string.replace("\n", "")

Where input_string is the string.

Example:

In this example, we will create a string with newline characters and remove it using the replace() method.

input_string = "welcome\n to\n thisPointer\n\n\n"

# Display actual string
print("Actual string: ",input_string)

# Remove newline characters using replace()
input_string = input_string.replace("\n", "")

print(input_string)

Output:

Actual string:  welcome
 to
 thisPointer



welcome to thisPointer

We can see that all newline characters from the string are removed.

It can also be possible to demonstrate this using list.

Example:

In this example, we will take 3 string elements in a list that contains trailing new lines each and will replace them as empty using replace().

input_list = ["welcome\n", "to\n", "thisPointer\n\n\n "] 

# Actual list
print(input_list)

#create a list
final = []

# use replace() that replaces \n with empty through for loop
for iterator in input_list:
    final.append(iterator.replace("\n", ""))

print(final)

Output:

['welcome\n', 'to\n', 'thisPointer\n\n\n ']
['welcome', 'to', 'thisPointer ']

Remove trailing newline characters using strip()

The strip() method of string class, deletes the newline characters and spaces from both beginning and end of the string. We can use this to remove the trailing newline characters in the given string.

Syntax:

input_string.strip()

Where input_string is the string from which we need to delete the trailing newline character.

Example:

In this example, we will create a string with a trailing newline character. Then we will remove that trailing newline character from string using the strip() method.

input_string = "welcome to thisPointer \n"

# Display actual string
print("Actual String: ", input_string)

# Remove trailing line using strip()
modified = input_string.strip()

# Display the modified string
print("removed trailing newline: ", modified)

Output:

Actual String:  Welcome to thisPointer 

removed trailing newline:  Welcome to thisPointer

Summary

In this Python tutorial, we learned about four methods to remove the trailing newlines characters from strings in Python. The strip() and rstrip() methods provide similar work in removing trailing newline characters from the string. The strings.replace() and sub() in the regex module are also used to remove trailing newlines from the strings.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top