4

How to remove a trailing newline in Python?

 1 year ago
source link: https://thispointer.com/how-to-remove-a-trailing-newline-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:

Advertisements

"Hello this is mystring\n"
"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()
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)
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
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)
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)
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
thisPointer
Removed Trailing characters: welcome
thisPointer
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)
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 ']
['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", "")
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)
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
thisPointer
welcome to thisPointer
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)
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 ']
['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()
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)
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
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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK