Huy Than
Huy Than

Reputation: 1546

Concatenate lines of form input into a single line

I have a HTML form, user will input here text (with lines), like this :

How are you?
I am fine. 
Thank you!

In the server side, I will get that input by :

input = request.POST['input']

And then I will to concatenate them into a single line like this :

How are you? I am fine.Thank you!

I have tried this but it doesn't work :

 input = request.POST['input']
 input = input.rstrip('\n')
 print(input) #print to test the concatenation

Please help me! Thank you!

Upvotes: 1

Views: 80

Answers (3)

Huy Than
Huy Than

Reputation: 1546

Thank you Harrison and Cychih for your answer, I tried however, they don't work. The solution I found is this :

newstring = input1.replace('\r\n', ' ')

It's

'\r\n'

not

'\n'

When I put in the form 3 lines like this ;

Line 1
Line 2
Line 3

Here is the output of the request.POST

<QueryDict: {'input1': ['Line 1 \r\nLine 2\r\nLine 3\r\n'], 'csrfmiddlewaretoken': ['tvC0ISHPNEkdQzocuVRnhJCp5yadDkgC5wf832blrpYPP0MZVV1iNY5bI2cYXsA4'], 'output1': [' ']}>

I don't know why new line is set as \r\n but it's the way it is .

Thank you again for your help!

Upvotes: 0

rafalmp
rafalmp

Reputation: 4068

Use

" ".join(input.split("\n"))

Upvotes: 1

Harrison
Harrison

Reputation: 5396

newstring = input.replace('\n', '') should work

Upvotes: 1

Related Questions