Input a multiline string in python [duplicate]

2019-08-08 10:37发布

问题:

This question already has an answer here:

  • How do I read multiple lines of raw input in Python? 7 answers

I am trying to create a simple programme that inputs a single multiline input and outputs it as a table.

input:

a
b
c

I know i could have done this is easily in python 2.7.5 by using:

input_string = raw_input("> ").splitlines()

this would return ['a', 'b', 'c']

however in python 3, the line:

input_string = input("> ").splitlines()

only returns ['a'] hence it only reads the first line of the input string.

Is there any way in python 3 to accept a multiline input? Thanks.

回答1:

just try.

aux = ''
'\n'.join(iter(input, aux))