Python Remove Newline From String

There are times where we need to remove the newline from string while processing massive data. This tutorial will learn different approaches to strip newline characters from string in Python.

Python Remove Newline From String

In Python new line character is represented with “\n.” Python’s print statement by default adds the newline character at the end of the string.

There are 3 different methods to remove the newline characters from the string. 

  1. strip() method
  2. replace() method
  3. re.sub() method

Using strip() method to remove the newline character from a string

The strip() method will remove both trailing and leading newlines from the string. It also removes any whitespaces on both sides of a string.

# strip() method to remove newline characters from a string
text= "\n Welcome to Python Programming \n"
print(text.strip())

Output

Welcome to Python Programming

If the newline is at the end of the string, you could use the rstrip() method to remove a trailing newline characters from a string, as shown below.

# rstrip() method to remove trailing newline character from a string
text= "Welcome to Python Programming \n"
print(text.rstrip())

Output

Welcome to Python Programming

Using replace() method to remove newlines from a string

The replace() function is a built-in method, and it will replace the specified character with another character in a given string. 

In the below code, we are using replace() function to replace the newline characters in a given string. The replace() function will replace the old character and substitute it with an empty one.

Similarly, if we need to replace inside newline characters in a list of strings, we can iterate it through for loop and use a replace() function to remove the newline characters.

# Python code to remove newline character from string using replace() method

text = "A regular \n expression is a sequence \n of characters\n that specifies a search\n pattern."
print(text.replace('\n', ''))

my_list = ["Python\n", "is\n", "Fun\n"]
new_list = []

print("Original List: ", my_list)

for i in my_list:
    new_list.append(i.replace("\n", ""))
print("After removal of new line ", new_list)

Output

A regular  expression is a sequence  of characters that specifies a search pattern. 
Original List:  ['Python\n', 'is\n', 'Fun\n']
After removal of new line  ['Python', 'is', 'Fun']

We can also use the map function in Python to iterate the list of strings and remove the newline characters, as shown below. It would be a more optimized and efficient way of coding when compared to the for a loop.

my_list = ["Python\n", "is\n", "Fun\n"]
print(list(map(str.strip, my_list)))

Output

['Python', 'is', 'Fun']

Using regex to remove newline character from string  

Another approach is to use the regular expression functions in Python to replace the newline characters with an empty string. The regex approach can be used to remove all the occurrences of the newlines in a given string.

The re.sub() function is similar to replace() method in Python. The re.sub() function will replace the specified newline character with an empty character.

# Python code to remove newline character from string using regex

import re
text = "A regular \n expression is a sequence \n of characters\n that specifies a search\n pattern."
print(re.sub('\n', '', text))

my_list = ["Python\n", "is\n", "Fun\n"]
new_list = []

print("Original List: ", my_list)

for i in my_list:
    new_list.append(re.sub("\n", "", i))
print("After removal of new line ", new_list)

Output

A regular  expression is a sequence  of characters that specifies a search pattern. 
Original List:  ['Python\n', 'is\n', 'Fun\n']
After removal of new line  ['Python', 'is', 'Fun']
Leave a Reply

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

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Type()

Python type()

Table of Contents Hide type() Syntaxtype() Parameterstype() Return ValueDifference between type() function and isinstance() functionExample of isinstance() method in PythonExample 1: Finding the type of a Python objectExample 2: type()…
View Post