Store multiline input in String (Python)

Input:

359716482
867345912
413928675
398574126
546281739
172639548
984163257
621857394
735492861

my code is:

print("Enter the array:\n")   
userInput = input().splitlines()
print(userInput)

my problem is that userInputit only accepts the value of the first row, but it doesn't seem to accept values ​​after the first row?

+3
source share
4 answers

You can use the method readlines()for file objects:

import sys
userInput = sys.stdin.readlines()
+12
source

You can easily create it using generators. Here is one such implementation. Note that you can either press blank return or any keyboard interrupt to break out of the entry point

>>> def multi_input():
    try:
        while True:
            data=raw_input()
            if not data: break
            yield data
    except KeyboardInterrupt:
        return


>>> userInput = list(multi_input())
359716482
867345912
413928675
398574126

>>> userInput
['359716482', '867345912', '413928675', '398574126']
>>> 
+5
source

input() . :

  • input() ,
  • input() , ctrl-D UNIX- , EOFError,
  • Read data from a text file or other more suitable source than stdin
+2
source
lines = []
while True:
s =input("Enter the string or press ENTER for Output: ")
if s:
    lines.append(s)
else:
    break;

print("OUTPUT: ")
for i in lines:
print (i)

Input:
359716482
867345912
413928675
398574126
546281739
172639548
984163257
621857394
735492861

Output:
359716482
867345912
413928675
398574126
546281739
172639548
984163257
621857394
735492861
-1
source

All Articles