close

How to remove newline from string in python

In python newline character defined by \n. And it is used to form a new line in your output. So basically here we are going to learn about How to remove newline from string in python. So lets see how it will work by these methods.

How to remove newline from string in python

From using below methods you can remove newline character from leading and trailing sides and there are also some method which is use for removing all the newline character. I hope you guys like it. Lets see it.

  1. remove newline from string in python

    to remove newline from string in python just Use strip().By using strip() you can remove newline from string. But by using this you can only remove leading and trailing newline. It will also remove the whitespace from leading and trailing sides. Lets learn this. str = "\n Hello guys. How are you?\n" newstr = str.strip() print(newstr) Output : Hello guys. How are you? This method also remove the extra white space at the leading and trailing sides. We can learn this by given below example: str = " \n Hello guys. How are you?\n " newstr = str.strip() print(newstr) Output : Hello guys. How are you?

  2. How to remove newline from string in python

    to remove newline from string in python just Use replace().To remove newline character you can use replace(). with the help of for loop you can use replace () function to replace the \n character. Lets learn this by an example: mylist = ["pizza\n","burger","dho\nsa"] rez = [] for x in mylist: rez.append(x.replace("\n", "")) print("New list : " + str(rez)) Output :
    New list : ['pizza', 'burger', 'dhosa']

Method 1: Use strip()

By using strip() you can remove newline from string. But by using this you can only remove leading and trailing newline. It will also remove the whitespace from leading and trailing sides. Lets learn this.

str = "\n Hello guys. How are you?\n"
newstr = str.strip()
print(newstr)

Output :

Hello guys. How are you?

This method also remove the extra white space at the leading and trailing sides. We can learn this by given below example:

str = "   \n Hello guys. How are you?\n    "
newstr = str.strip()
print(newstr)

Output :

Hello guys. How are you?

Method 2: Use replace()

To remove newline character you can use replace(). with the help of for loop you can use replace () function to replace the \n character. Lets learn this by an example:

mylist = ["pizza\n","burger","dho\nsa"]
rez = []
for x in mylist:
    rez.append(x.replace("\n", ""))
print("New list : " + str(rez))

Output :

New list : ['pizza', 'burger', 'dhosa']

Method 3: Use re.sub()

Re.sub() function simply replace the newline character into another substring. Thus by using this we can remove newline character very easily. lets learn about of this.

import re
mylist = ["pizza\n","burger","dho\nsa"]  
rez = []
for sub in mylist:
    rez.append(sub.replace("\n", ""))          
print("New List : " + str(rez))

Output :

New List : ['pizza', 'burger', 'dhosa']

Method 4: Using regex

By using regex function you can remove newline character very easily. This is very occurreted method because other method remove one occurrence but this method remove every occurrence. Lets learn about of how it works.

import re
mylist = ["pizza\n","burger","dho\nsa"]  
print("The original list : " + str(mylist))
res = []
for sub in mylist:
    res.append(re.sub('\n', '', sub))
print("List after newline character removal : " + str(res))

Output :

The original list : ['pizza\n', 'burger', 'dho\nsa']
List after newline character removal : ['pizza', 'burger', 'dhosa']

Method 5: Using loop

For remove newline character you can use a loop. By using for loop you can easily replace newline character. Lets learn this.

mylist = ["pizza\n","burger","dho\nsa"]
print("The original list : " + str(mylist))
res = []
for sub in mylist:
    res.append(sub.replace("\n", ""))
print("List after newline character removal : " + str(res))

Output :

The original list : ['pizza\n', 'burger', 'dho\nsa']
List after newline character removal : ['pizza', 'burger', 'dhosa']

Conlusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment