I want to write a program that gets multiple line input and work with it line by line. Why isn't there any function like raw_input in Python 3? input does not allow the user to put lines separated by newline (Enter). It prints back only the first line.
Can it be stored in a variable or even
read it to a list? asked May 14, 2015 at 13:49 7 raw_input can correctly handle the EOF, so we can write a loop, read till we have received an EOF (Ctrl-D) from user:
Python 3print("Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it.")
contents = []
while True:
try:
line = input()
except EOFError:
break
contents.append(line)
Python 2print "Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it."
contents = []
while True:
try:
line = raw_input("")
except EOFError:
break
contents.append(line)
answered
Jul 6, 2016 at 11:47 xiaketxiaket
1,66313 silver badges8 bronze badges 4 In Python 3.x
the raw_input() of Python 2.x has been replaced by input() function. However in both the cases you cannot input multi-line strings, for that purpose you would need to get input from the user line by line and then .join() them using \n , or you can also take various lines and concatenate them using + operator separated by \n To get multi-line input from the user you can go like: no_of_lines = 5
lines = ""
for i in xrange(no_of_lines):
lines+=input()+"\n"
print(lines)
Or lines = []
while True:
line = input()
if line:
lines.append(line)
else:
break
text = '\n'.join(lines)
AMGMNPLK 1,7042 gold badges11 silver badges21 bronze badges answered May 14, 2015 at 13:51 ZdaRZdaR 21.2k6 gold badges61 silver badges83 bronze badges 3 input(prompt) is basically equivalent to
def input(prompt):
print(prompt, end='', file=sys.stderr, flush=True)
return sys.stdin.readline()
You can read directly from sys.stdin if you like. lines = sys.stdin.readlines()
lines = [line for line in sys.stdin]
five_lines = list(itertools.islice(sys.stdin, 5))
The first two require that the input end somehow, either by reaching the end of a file or by the user typing Control-D (or Control-Z in Windows) to signal the end. The last one will return after five lines have been read, whether from a file or from the terminal/keyboard. answered May 14, 2015 at 14:45 chepnerchepner 460k66 gold badges483 silver badges624 bronze badges 9 Use the input() built-in function to get a input line from the user. You can read the help here. You can use the following code to get several line at once (finishing by an
empty one): while input() != '':
do_thing
answered May 14, 2015 at 13:52 maggickmaggick 1,30413 silver badges23 bronze badges 1 no_of_lines = 5
lines = ""
for i in xrange(5):
lines+=input()+"\n"
a=raw_input("if u want to continue (Y/n)")
""
if(a=='y'):
continue
else:
break
print lines
answered Jul 5, 2016 at 5:10
1 |