I want to get input from the user in python, but the input that is pasted in from the user has multiple new lines in it, so if I enter it as input, the new lines stop the input and I don't get the full input. For example if my input is hello\nworld
(not with a literal \n
but there is a new line in between the words), hello
is used as the input and when the program is done executing, the terminal executes world
like a command, which there is none. One way I was thinking about solving this is to have a forever loop of inputs and then combine the inputs from the multiple inputs, but that doesn't seem like a great solution because you have no good way of exiting out of the forever loop because at the end, there is another input running. Is there any way to get input from the user even if there are these breaks in them?
Asked
Active
Viewed 500 times
0

kjdfsjdsa kafsjna
- 31
- 5
-
Does this answer your question? [How to get multiline input from the user](https://stackoverflow.com/questions/30239092/how-to-get-multiline-input-from-the-user) – Cow Aug 12 '22 at 05:45
2 Answers
0
to get this task done, we can use sys.stdin instead of input(). we can use read() method (that reads all lines) combined with splitlines(). the output is a list of the input data without any new-line sequence.
import sys
inp = sys.stdin.read().splitlines()
print(inp)

lroth
- 367
- 1
- 2
- 4
0
you can get multiple line using a little logic
print "Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it."
lines = []
while True:
line = input()
if line:
lines.append(line)
else:
break
text = '\n'.join(lines)

Hari
- 334
- 4
- 16