How to remove newlines from a string in Python 3?

To remove new line characters from a string in Python, use the replace() or the strip() functions. Here is an example of the code:

your_str = your_str.replace('/n', '')

# alternatively

your_str = your_str.strip()

Strip newlines / line breaks from strings – Practical Example

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

#1 – Using replace() method:

If you have a string that contains multiple line breaks, you can use the replace method and get multiple newlines removed / replaced.

Code:

mystring = 'This is my string \nThis comes in the next line.'
print("With line breaks:" + mystring)
print("After deleting line breaks:",mystring.replace('\n',''))

Output:

With line breaks:This is my string 
This comes in the next line.
After deleting line breaks: This is my string This comes in the next line.

#2 -Using strip() method:

The Python strip() function removes any trailing character at the beginning and end of the string. So if your brealines are located before or after the string you can use strip() to get rid of them.

Code:

mystring = '\nThis is my string. \n'
print("With newlines:" + mystring)
print("After deleting the newlines:",mystring.strip())

Output:

With newlines:
This is my string. 

After deleting the newlines: This is my string.

#3 -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:

mystring = 'This is my string \nThis comes in the next line.'
print("With line breaks:" + mystring)
print("After deleting new lines:",''.join(mystring.splitlines()))

Output:

With line breaks:This is my string 
This comes in the next line.
After deleting new lines: This is my string This comes in the next line.

Replace line breaks with space

Another common case is to place empty spaces instead of newlines in a string. Let’s look at a simple example:

my_str = 'This is a string that\ni read from a file\n'
print(my_str)

This will return the following:

This is a string that
i read from a file

Let’s replace the line breaks with a space and print the result:

print(my_str.replace('\n', ' '))

This will return:

This is a string that i read from a file 

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]

Remove Line Breaks from a file

Last topic for this tutorial is removing newlines from the contents of a text file.

We know how to read text files into a Python list using readlines(). In this case, we need a somewhat different approach. We first want to read contents of the text file into a string. This is easily accomplished by the file object read() function. We can then manipulate the string as needed, in this case replacing the newline characters with a space.

with open (file_path, 'r') as f:
    print(f.read().replace('\n',' '))

Related learning

How to read text files into pandas DataFrames?