How do you remove n from string in python 3?

Problem

You have a very long string or one that include newline escape characters (\n). You would like to use Python 3 in order to automatically delete those appended newlines added to your string.

In this post, we will outline three methods that you can use to delete newlines from a string. In this post we’ll discuss each technique and post example code that you can use to follow along.

Using rstrip() method:

The rstrip() method removes any trailing character at the end of the string. By using this method, we can remove newlines in the provided string value.

Code:

def removelines(value):
    return value.rstrip()

mystring = 'This is my string. \n'
print("Actual string:",mystring)
print("After deleting the new line:",removelines(mystring))

Output:

Actual string: This is my string

After deleting the new line: This is my string.

Using replace() method:

To remove any of the newlines found between a string, we can use the replace method and get the newline removed.

Code:

def removelines(value):
    return value.replace('\n','')

mystring = 'This is my string \nThis comes in the next line.'
print("Actual string:",mystring)
print("After deleting the new line:",removelines(mystring))

Output:

Actual string: This is my string
This comes in the next line.
After deleting the new line: This is my string This comes in the next line.

Using splitlines() method:

The splitlines() method helps to convert the lines into a split list. Hence, we can split our string into a list and then join it to form a string value.

Code:

def removelines(value):
    return ''.join(value.splitlines())

mystring = 'This is my string \nThis comes in the next line.'
print("Actual string:",mystring)
print("After deleting the new line:",removelines(mystring))

Output:

Actual string: This is my string
This comes in the next line.
After deleting the new line: This is my string This comes in the next line.

Removing newlines from a Python list

In a similar fashion you can easily strip newlines off a list of strings.

Let’s assume you have the following list:

orig_lst = ['Python', 'R', 'GO\n', 'Haskell']

We can easily strip the newlines off the list elements with a list comprehension and the rstrip() function:

new_lst = [x.rstrip() for x in orig_lst]
print(new_lst)

Here’s is the result:

['Python', 'R', 'GO', 'Haskell']

Alternatively we can obtain the same result by using the replace() function:

new_lst = [x.replace('\n','') for x in orig_lst]

We can also replace the newline characters with a space:


new_lst = [x.replace('\n',' ') for x in orig_lst]

I'm bubbling up my regular expression based answer from one I posted earlier in the comments of another answer. I think using re is a clearer more explicit solution to this problem than str.rstrip.

>>> import re

If you want to remove one or more trailing newline chars:

>>> re.sub(r'[\n\r]+$', '', '\nx\r\n')
'\nx'

If you want to remove newline chars everywhere (not just trailing):

>>> re.sub(r'[\n\r]+', '', '\nx\r\n')
'x'

If you want to remove only 1-2 trailing newline chars (i.e., \r, \n, \r\n, \n\r, \r\r, \n\n)

>>> re.sub(r'[\n\r]{1,2}$', '', '\nx\r\n\r\n')
'\nx\r'
>>> re.sub(r'[\n\r]{1,2}$', '', '\nx\r\n\r')
'\nx\r'
>>> re.sub(r'[\n\r]{1,2}$', '', '\nx\r\n')
'\nx'

I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either \r\n or \n and nothing more.

>>> re.sub(r'(?:\r\n|\n)$', '', '\nx\n\n', count=1)
'\nx\n'
>>> re.sub(r'(?:\r\n|\n)$', '', '\nx\r\n\r\n', count=1)
'\nx\r\n'
>>> re.sub(r'(?:\r\n|\n)$', '', '\nx\r\n', count=1)
'\nx'
>>> re.sub(r'(?:\r\n|\n)$', '', '\nx\n', count=1)
'\nx'

(The ?: is to create a non-capturing group.)

(By the way this is not what '...'.rstrip('\n', '').rstrip('\r', '') does which may not be clear to others stumbling upon this thread. str.rstrip strips as many of the trailing characters as possible, so a string like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

How do you get rid of N in a string in Python?

Remove \n From the String in Python Using the str. strip() Method. In order to remove \n from the string using the str. strip() method, we need to pass \n and \t to the method, and it will return the copy of the original string after removing \n and \t from the string.

How do you remove n from a list in Python 3?

Use str. strip() to remove newline characters from a list. Use a for-loop to iterate through the elements of a list, and call str. strip() with str as each element to return a copy of the element with leading and trailing whitespace as well as newline characters removed.

What does '\ n do in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text.

How do you replace N in Python?

Use the replace() Function to Replace Newline With Space in Python.
Copy string1 = 'Hello\nWorld'.
Copy print(string1).
Copy string2 = string1. replace('\n', ' ').
Copy print(string2).