In this article, we will learn different ways how to remove the newline character from strings in python.
While working with python, we might need to remove new lines from strings while processing some data. A newline in python string is represented by the newline character n
.
What is a Newline character in python?
In python, the newline character (n
) is a character that represents a line break in a string. It is used at the end of the line to let the program know that the new text of a string should start from a new line.
In python documentation, it is mentioned that the print statement add the newline character at the end of the string.
Let’s see an example of how a newline works in a string.
str = "Hello, this is first line. nThis is second"
print(str)
Output:
Hello, this is first line.
This is second
As you can see, the sentence after the n
character is printed on a new line.
Different Ways to Remove Newline character in Python
In Python, there are three different ways by which we can remove the newline from a given string.
- Using strip() method
- Using replace() method
- Using re.sub() function
Let’s understand each method with examples.
Using replace() to remove python newline
The replace()
is an in-built python method that is used to replace specific characters with another character in a string.
string.replace(oldval, newval)
Let’s see an example where we will remove newline character and replace it with space.
str = "Hello, this is first line. nThis is second.nThird line."
print(str.replace('n',' '))
Output:
Hello, this is first line. This is second. Third line.
If you have a list of string then we have to use for
loop and remove the newline character from each string.
str_list = ["hellon","This is men","JohnnWick"]
new_str=[]
for str in str_list:
new_str.append(str.replace('n',' '))
print(new_str)
Output:
['hello ', 'This is me ', 'John Wick']
Here, we have iterated through the list of strings (str_list
) and removed the newline from each string using replace()
method. And then append the new string to the new_str
list.
Remove newline from string using strip() method
In python, the strip()
method is used to remove the leading and trailing characters (whitespace or any user-specified characters) from a string. It can also be used to remove newline from the beginning and the end of a string.
Syntax:
string.strip(characters)
We can pass the character
we want to remove from the string as the parameter. Here we will pass n
character.
Let’s see an example to remove newline from a string using strip()
method.
str = "n This is long string in python n"
print(str.strip('n'))
Output:
This is long string in python
In this above example, we have removed the newline character (n
) from the beginning and end of the string using strip
method.
To remove only the trailing character i.e the character at the end of a string, we can use the rstrip()
method.
str = "n This is long string in python n"
print(str.rstrip('n'))
Output:
This is long string in python
And if you want to remove only the leading character i.e the character at the beginning of the string, we can use the lstring()
method.
str = "n This is long string in python n"
print(str.lstrip('n'))
Output:
This is long string in python
Using re.sub() to remove newline character from a string
We can also use the regular expression to remove and replace a newline character from a string in python.
The re.sub()
function is used to find and replace characters in a string. We will use this function to replace the newline character with an empty character in a given string in python.
To use the regular expression in python, we have to import the re
library first in the program.
import re
str = "nThis is first.n This is second.n"
new_str = re.sub('n','', str)
print(new_str)
Output:
This is first. This is second.
Here, in the above example, we have first imported the re
library first and then used the re.sub()
function.
The re.sub()
function takes in three parameters:
- First, the character we want to replace in the string. Here
n
. - The second parameter is the character we want to replace with, and
- The string on which we want to apply the changes.
This function creates a new string without making any changes to the original one.
Conclusion: In this article, we have learned how to remove newline characters from the strings using three different methods in python.
Related Topics:
Replace character in String by index in Python
Python – Check if a string is Empty or Not
Split string by whitespace in python
Convert a string into integer or float using Python
Split String and get the first element using python
Solving TypeError: string indices must be integers error in Python