How to Remove all Newlines from String : Various Methods

How to Remove all Newlines from String

Suppose you have a string with newlines in it and want to remove the newline character. Then how you can do so? In this tutorial, you will learn how to remove all newlines from strings in python using various methods.

Methods to remove all newlines from String

In this section, you will know all the methods to remove newline characters from a particular string. But before that let’s create a sample string with the newlines character.

Use the below line of code to create a sample string.

Now let me know the methods to remove all newlines.

Method 1: Using the replace() function

The first method to remove all newlines from the string is the use of replace() function. Here you will pass the newline character with the blank separator. It will remove the newline character with the blank space.

Run the below lines of code.

sample_string = "Data\n Science\n Learner"
new_string = sample_string.replace("\n", "")
print(new_string)

Output

removing newlines using replace() function
removing newlines using replace() function

Method 2 : Using join() and split() function

In this method, you will first split the string using the split() function on the newline character. After that, you will join all the strings using the join() function.

Use the below lines of code to split and join all the strings.

sample_string = "Data\n Science\n Learner"
new_string = "".join(sample_string .split("\n"))
print(new_string)

Output

removing newlines using slit and join function
removing newlines using slit and join function

Method 3: Remove all newlines from the string using regex

The third method to remove all newlines is the use of the regex module. This module provides a sub() function that will accept three arguments. One is the character you want to remove, the second is the blank space and the third is the input string.

It uses the regular expression to match the “\n” character and removes all of it with blank space.

Execute the below lines of code to achieve the result.

import re
sample_string = "Data\n Science\n Learner"
new_string = re.sub("\n", "", sample_string )
print(new_string)

Output

removing newlines using regex expression
removing newlines using regex expression

Conclusion

Sometimes there is a character in a string that you want to remove. If you want to remove the newline character then the above method will work for you. However, using the above method you can remove any characters inside the string just because you have defined the character in each method.

That’s all for now. I hope you have liked this tutorial. If you want to add any methods to this post or have a suggestion then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner