Remove all line breaks from a long line of text

Basically, I ask the user to enter a line of text in the console, but the line is very long and includes many line breaks. How to take a user line and remove all line breaks to make it one line of text. My method for getting the string is very simple.

string = raw_input("Please enter string: ") 

Is there any other way to capture a string from a user? I am running Python 2.7.4 on a Mac.

PS Obviously, I'm noob, so even if the solution is not the most efficient, one that uses the simplest syntax will be evaluated.

+103
python
May 15, '13 at 13:25
source share
7 answers

How do you get line breaks using raw_input ? But, as soon as you have a string with some characters in it, you want to get rid, just replace them.

 >>> mystr = raw_input('please enter string: ') please enter string: hello world, how do i enter line breaks? >>> # pressing enter didn't work... ... >>> mystr 'hello world, how do i enter line breaks?' >>> mystr.replace(' ', '') 'helloworld,howdoienterlinebreaks?' >>> 

In the above example, I replaced all the spaces. Line '\n' represents newline characters. And \r represents the carriage return (if you are in windows, you can get them, and the second replace will handle them for you!).

mostly

 # you probably want to use a space ' ' to replace `\n` mystring = mystring.replace('\n', ' ').replace('\r', '') 

Note also that it is a bad idea to call your string variable, as this obscures the string module. Another name I would avoid but would like to use sometimes: file . For the same reason.

+164
May 15 '13 at 13:28
source share

You can try using string replacement:

 string = string.replace('\r', '').replace('\n', '') 
+28
May 15, '13 at 13:28
source share

You can split a line without the separator arg argument, which will treat consecutive spaces as one separator (including newlines and tabs). Then join the space:

 In : " ".join("\n\nsome text \r\n with multiple whitespace".split()) Out: 'some text with multiple whitespace' 

https://docs.python.org/2/library/stdtypes.html#str.split

+18
May 03 '16 at 10:24
source share

updated based on Xbello comment:

 string = my_string.rstrip('\r\n') 

more details here

+14
Sep 24 '14 at 9:43
source share

Another option is a regular expression:

 >>> import re >>> re.sub("\n|\r", "", "Foo\n\rbar\n\rbaz\n\r") 'Foobarbaz' 
+4
May 31 '18 at 11:36
source share

Tailored method

  • extra white characters at the beginning / end of the line
  • extra white characters at the beginning / end of each line
  • various end of line characters

a multiline string is required that can be dirty, for example

 test_str = '\nhej ho \n aaa\r\na\n ' 

and produces a good single line string

 >>> ' '.join([line.strip() for line in test_str.strip().splitlines()]) 'hej ho aaa a' 

UPDATE: To fix multiple newlines that produce redundant spaces:

 ' '.join([line.strip() for line in test_str.strip().splitlines() if line.strip()]) 

This works for the next test_str = '\nhej ho \n aaa\r\n\n\n\n\na\n '

+3
Mar 01 '17 at 10:42 on
source share

The problem with rstrip is that it does not work in all cases (as I myself have seen a few). Instead, you can use - text = text.replace ("\ n", ""), this will delete the whole new line \ n with a space.

Thank you guys in advance for your votes.

0
May 7 '19 at 8:52
source share



All Articles