0

i am trying to get input from user in multiple lines when user entered multiple line input then i stored in a variable buffer. and to print same multiline input i add \n after every iteration. now i want to find index of \n.
import re

buffer = ''
while True:
    line = raw_input()
    if not line: break


    buffer +='\n'
    buffer += line
nlf="\\n"
nl=buffer.find(nlf)
print nl

i join two lines but i want to find index \n where user press enter?

1 Answer 1

2

Set nlf to the line break itself instead: '\n':

nlf = '\n'

Btw. it’s usually not recommend to do string concatenation like this (see this related question), so consider using a list instead:

lines = []
while True:
    line = raw_input()
    if not line:
        break
    lines.append(line)

That the way you end up with a list of all lines (so you don’t even need to find the line break). And if you want the full text including line breaks later, you can do this:

text = '\n'.join(lines)
Sign up to request clarification or add additional context in comments.

4 Comments

That’s because the first line break actually is at index 0. The first thing you add to the (empty) buffer is the line break in loop. If you want to skip that one, you can pass the index you want to start searching from as the second parameter to str.index: buffer.index('\n', 1).
what if we want same multiline output ?
Multiline output? What do you mean?
for example if input is in 3 lines then output also in 3 lines not like a list

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.