63

I'm trying to remove all newline characters from a string. I've read up on how to do it, but it seems that I for some reason am unable to do so. Here is step by step what I am doing:

string1 = "Hello \n World"
string2 = string1.strip('\n')
print string2

And I'm still seeing the newline character in the output. I've tried with rstrip as well, but I'm still seeing the newline. Could anyone shed some light on why I'm doing this wrong? Thanks.

smci
  • 32,567
  • 20
  • 113
  • 146
LandonWO
  • 1,249
  • 3
  • 14
  • 16
  • 8
    Naming `str` a variable is not necessarily a good idea, considering `str` is a [built-in](http://docs.python.org/2/library/functions.html#str) function. – Nadir Sampaoli Nov 08 '12 at 22:27

8 Answers8

102

strip only removes characters from the beginning and end of a string. You want to use replace:

str2 = str.replace("\n", "")
re.sub('\s{2,}', ' ', str) # To remove more than one space 
kamran kausar
  • 4,117
  • 1
  • 23
  • 17
mipadi
  • 398,885
  • 90
  • 523
  • 479
30

As mentioned by @john, the most robust answer is:

string = "a\nb\rv"
new_string = " ".join(string.splitlines())
Chris Sewell
  • 679
  • 1
  • 7
  • 19
7

Answering late since I recently had the same question when reading text from file; tried several options such as:

with open('verdict.txt') as f: 

First option below produces a list called alist, with '\n' stripped, then joins back into full text (optional if you wish to have only one text):

alist = f.read().splitlines()
jalist = " ".join(alist)

Second option below is much easier and simple produces string of text called atext replacing '\n' with space;

atext = f.read().replace('\n',' ')

It works; I have done it. This is clean, easier, and efficient.

zondo
  • 19,901
  • 8
  • 44
  • 83
john
  • 71
  • 1
  • 1
4

strip() returns the string after removing leading and trailing whitespace. see doc

In your case, you may want to try replace():

string2 = string1.replace('\n', '')
venkatvb
  • 681
  • 1
  • 9
  • 24
sradack
  • 149
  • 1
3

or you can try this:

string1 = 'Hello \n World'
tmp = string1.split()
string2 = ' '.join(tmp)
venkatvb
  • 681
  • 1
  • 9
  • 24
jkalivas
  • 1,117
  • 1
  • 8
  • 11
1

This should work in many cases -

text = ' '.join([line.strip() for line in text.strip().splitlines() if line.strip()])
text = re.sub('[\r\n]+', ' ', text)
joydeba
  • 778
  • 1
  • 9
  • 24
-1

strip() returns the string with leading and trailing whitespaces(by default) removed.

So it would turn " Hello World " to "Hello World", but it won't remove the \n character as it is present in between the string.

Try replace().

str = "Hello \n World"
str2 = str.replace('\n', '')
print str2
venkatvb
  • 681
  • 1
  • 9
  • 24
-1

If the file includes a line break in the middle of the text neither strip() nor rstrip() will not solve the problem,

strip family are used to trim from the began and the end of the string

replace() is the way to solve your problem

>>> my_name = "Landon\nWO"
>>> print my_name
   Landon
   WO

>>> my_name = my_name.replace('\n','')
>>> print my_name
LandonWO
ibmstafa
  • 71
  • 1