Remove Newline from a String in Python

Like many programming languages, Python has a newline character, represented by \n, which indicates that the string should be displayed in more than one line. If you look at the documentation of the print buil-in function, you’ll see that Python adds it, by default, at the end of any print() statement.

In this tutorial, we’ll see what options you have at your disposal to remove a newline from a string.

Use strip() to Remove Newlines From a String in Python

One of the most common and easiest way to perform this operation is to use the built-in strip() method, which removes leading and trailing characters from a string. By default, and if the chars argument is not specified, it will remove spaces & newlines.

string = '\nI am happy\n' print(string.strip())
Code language: Python (python)

Output:

I am happy

You can also use rstrip() or lstrip() to remove only trailing or leading newlines. This is not very common because you’ll often want to remove all occurrences of a newline in your string, but it can sometimes be useful.

Use replace() to Remove Newlines From a String in Python

In some cases, newlines won’t be only at the beginning and at the end of your string; therefore our previous method won’t be enough. We can use the replace() method (useful to remove a substring from a string) to achieve our objective.

#our string string = '\nI am happy\n and I hope you \n are as well' #we convert it to a list of words words = string.split(' ') #we remove newlines from our words words_no_newline = [word.replace('\n','') for word in words] #we recreate our string string_no_newline = ' '.join(words_no_newline) print(string_no_newline)
Code language: Python (python)

Output:

I am happy and I hope you are as well
Code language: JavaScript (javascript)

Use RegEx to Remove Newlines From a String in Python

If you like working with regular expressions, you can also use the re module to achieve the same thing. The re.sub() function allows us to replace a substring by another one (or remove it), which is what we want to achieve here.

#import the library import re #original string string = '\nI am happy\n and I hope you \n are as well' #we convert it to a list of words words = string.split(' ') #we remove newlines from our words string_no_newline = ' '.join([word.replace('\n','') for word in words]) print(string_no_newline)
Code language: Python (python)

The code is similar to the one we used in the previous section, but you can leverage the power of the re module by using a more complex regular expression if you need to.

Output:

I am happy and I hope you are as well
Code language: JavaScript (javascript)

Use splitlines() to Remove Newlines From a String in Python

Finally, the splitlines() method is maybe the most efficient way to achieve our objectives because it will remove leading, trailing and other newlines with a just one line of code. What else do you need?

string = '\nI am happy\n and I hope you \n are as well' string_no_newline = ' '.join(string.splitlines()) print(string_no_newline)
Code language: PHP (php)

Output:

I am happy and I hope you are as well
Code language: JavaScript (javascript)

Leave a Reply

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