I think we or you are finding your “finish” constraint confusing. You want to end input text. For that text to be finite, Python needs to know when it ends. How do you want to indicate when it ends? Answer from cameron on discuss.python.org
🌐
Reddit
reddit.com › r/learnpython › multi-line input in python 3
r/learnpython on Reddit: Multi-line input in Python 3
June 15, 2019 -

I'm trying to feed a prompt to a GPT-2 model, which is trained on song lyrics. With this model, you can feed it a prompt and it will attempt to finish it. The problem I'm having is that I need for the prompt to be formatted correctly like song lyrics - with each lyric on a newline. Since this model has been trained on formatted lyrics, the prompt needs to look the same to produce above average results.

input() doesn't work, because hitting return submits the text. I've also tried sys.stdin.read() as well and nothing seems to happen, just returns ' '.

Is it because I'm running it in a notebook? I'm only like 8 months deep into this python hobby, so I could be asking for something that can't happen.

Here's the code, would appreciate any ideas you've got.

prompt = input('Input Prompt:')
gpt2.generate(sess,
              length=200,
              temperature=.99,
              prefix= prompt, #this is where the prompt goes
              nsamples=5,
              top_p=.9,
              batch_size=5,
              run_name='run1'
              )
Discussions

python - How to get multiline input from the user - Stack Overflow
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... More on stackoverflow.com
🌐 stackoverflow.com
python - How to create mutiple list from mutiple lines input - Stack Overflow
@MatBailie I wrote it pasted in, but idk ab the ` escapes. Are you saying to iterate over input().splitlines()` to check if its alr split (ie escape chars)? It might just be my terminal but pasting in multilines does the same as typing them out, ie only the first line in captured by input() ... More on stackoverflow.com
🌐 stackoverflow.com
Store an input of multiple lines into a list in python - Stack Overflow
I have a simple question, I just want to store an input of multiple lines into an array using python, note that the first line of the input is telling me how many lines there will be, so if the first line of the input is 4, the whole input will be of 5 lines in total. ... I tried using n = list(m... More on stackoverflow.com
🌐 stackoverflow.com
Python: Multiline input converted into a list - Stack Overflow
I have a programming assignment and all of the inputs that I need to enter are multilined. For example: ... I am trying to convert the lines into lists. More on stackoverflow.com
🌐 stackoverflow.com
September 20, 2013
Top answer
1 of 3
2

Keep calling the input() function until the line it reads in is empty. The use the .split method (no args = space as deliminator). Use a list-comp to convert each string to an int and append this to your examList.

Here's the code:

examList = []
i = input()
while i != '':
    examList.append([int(s) for s in i.split()])
    i = input()

And with your input, examList is:

[[3], [2, 1], [1, 1, 0], [2, 1, 1], [4, 3, 0, 1, 2], [2], [1, 2], [1, 3]]

The above method is for Python3 which allows you to call input() and enter nothing (which is why we use this as a signal to check that we are done - i != '').

However, from the docs, we see that, in Python2, an empty entry to input() throws an EOF error. To get around this, I guess we could just make it so that the multi-line input is ended when the string such as: END is entered. This means that you must stop the entry with a line saying 'END':

examList = []
i = raw_input()
while i != 'END':
    examList.append([int(s) for s in i.split()])
    i = raw_input()

Note that I've used raw_input as to not perform type conversion.

which works the same as above.

2 of 3
2

You can use sys.stdin.readlines().

For example, start with

import sys
user_input = sys.stdin.readlines()

at this point, the value of user_input should be

['3\n', '2 1\n', '1 1 0\n', '2 1 1\n', '4 3 0 1 2\n', '2\n', '1 2\n', '1 3']

then, you should be able to get your desired output by performing the following processing

examList = []

for input_string in user_input:
   examList.append([int(value) for value in input_string.split()])

What we are doing here is iterating through user_input. For each input_string, we split() the words, convert them to int and put them back into a new list. Then, the new list will be added into examList.

🌐
GeeksforGeeks
geeksforgeeks.org › taking-multiple-inputs-from-user-in-python
Taking multiple inputs from user in Python - GeeksforGeeks
December 3, 2024 - The split() method divides the ... the values. If you want to collect multiple inputs from the user one at a time, you can use a loop....
🌐
Bobby Hadz
bobbyhadz.com › blog › python-input-multiple-lines
Multiple lines user Input in Python | bobbyhadz
On each iteration, append the user input and a newline character to a list. If the user presses Enter without typing in a value, break out of the loop. ... Copied!lines = [] while True: user_input = input() # 👇️ if the user presses Enter ...
🌐
Profound Academy
profound.academy › python-introduction › multi-line-lists-BLTEtz0sDYdm0zAjIB7S
multi-line lists - Introduction to Python
November 2, 2024 - This creates a list with 2 elements which are read from the input. ... Create a list of 8 integers that are read from the user input. Multiply those values by 2 and print the list in the output.
🌐
Sololearn
sololearn.com › en › Discuss › 2881136 › how-to-get-multiline-input-from-user
How to get multi-line input from user? | Sololearn: Learn to code for FREE!
September 13, 2021 - The best way is to create a list and append each input to that list by the append method : listname.append(input) And for the Unlimited inputs you need While True and for the limited input you need( for i in range(argument) ) ... # Python program ...
Find elsewhere
🌐
StudySmarter
studysmarter.co.uk › python multi input
Python Multi Input: Techniques & Examples | StudySmarter
One common approach to processing multi-line input is to store each line as an element in a list. This facilitates batch processing and subsequent data manipulation. Below is an example of collecting multiple lines of input until a specific termination word is input: inputs = []print('Please ...
🌐
Delft Stack
delftstack.com › home › howto › python › python input multiple lines
How to Get Multiple-Line Input in Python | Delft Stack
February 20, 2025 - The raw_input() function can be utilized to take in user input from the user in Python 2. However, the use of this function alone does not implement the task at hand. The primary purpose of using iter() with raw_input() is to create a loop that ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-multi-line-statements
Python - Multi-Line Statements - GeeksforGeeks
May 10, 2023 - In this example, we are initializing the list and the mathematical expression using the parentheses ( ), brackets [ ], and braces { } sign which is the implicit line continuation to continue the same line in the multiple lines in python programming.
🌐
Scaler
scaler.com › home › topics › how to take multiple input in python?
How to Take Multiple Input in Python? - Scaler Topics
March 30, 2024 - In case 1, we are asking the user to input two values of two variables in a single line separated by space as we have not mentioned the delimiter. Similarly, we obtain the input for three variables in case 2 where we give white space as a delimiter in subcase 1 and the delimiter as "," in subcase 2 to take multiple input in Python. In both these cases, we use the list comprehension and represent it by using the square brackets and for loop in addition to the split function to obtain the individual values for each variable by inputting the value in a single line.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-multi-line-statements
Multi-Line Statements in Python - GeeksforGeeks
June 30, 2025 - Implicit line continuation allows long statements to be split across multiple lines as long as the code is enclosed within parentheses ( ), brackets [ ], or braces { }. ... Explanation: uses (), [] and () to split a string, a list and a math ...
🌐
GeeksforGeeks
geeksforgeeks.org › input-multiple-values-user-one-line-python
How to input multiple values from user in one line in Python? - GeeksforGeeks
April 20, 2025 - Explanation: This code takes an integer n as the number of inputs, then uses a loop to read and convert each input to an integer, appending them to list a. ... Python Tutorial – Python is one of the most popular programming languages.
🌐
Learn By Example
learnbyexample.org › taking-multiline-input-from-a-user-in-python
Taking multiline input from a user in Python - Learn By Example
April 10, 2024 - For a more Pythonic solution, consider using a list comprehension with iter() function: print("Enter your lines. Type 'END' to finish.") lines = [line.rstrip() for line in iter(input, 'END')] # Assuming 'END' as the terminator print("You entered:") for line in lines: print(line) ... Enter your lines. Type 'END' to finish. Hi Hello How are you? END Your multiline input: Hi Hello How are you?
🌐
Reddit
reddit.com › r/learnpython › how do i process a multiple line input from "for line in sys.stdin:"?
r/learnpython on Reddit: How do I process a multiple line input from "for line in sys.stdin:"?
September 28, 2024 -

I recently took a coding assessment where I was tasked to compute the square of the input which is relatively easy. The issue was that the input was a multiple line input:
7
16

and the expected output is
49
256

The given code was

for line in sys.stdin:
    # your code here
    print(line, end="")

I tried to do ls = list(line.split()) but ls[0] is
['7']
['16']
and ls[1] is None

I also tried ls = list(line.split('\n')) but ls is
['7', '']
['16', '']

So how was I supposed to process the input to get ['7', '16'] rather than a 2 dimensional list?

From there I know how continue with make it an integer using map, creating a for loop for each item of the list and printing the square of each item.

I dont have a picture of the question since I was monitored on webcam but this is roughly what I remembered from the question.

edit: It was an online assessment platform so I am not sure exactly how the input was written as (like the raw input). Also I can only modify the code inside for line in sys.stdin:

Also, does anyone know how to write the input for sys.stdin using jupyternotebook such that I can practice this problem?