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 3

print("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 2

print "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)
Answer from xiaket on Stack Overflow
Discussions

Multi-line input in Python 3

To take multiple lines of input and put them in a list of strings, you can use sys.stdin.readlines() which will keep accepting returns until you give it an EOF [ctrl + d in *nix, ctrl + z in Windows].

E.g.:

    user_input = sys.stdin.readlines("Favourite foods followed by EOF:") # User gives input... 
    print(user_input)
Output:
    >> ["Spam\n", "Spam\n", "Spam\n"]
Obviously this leaves you with newlines, so you could do something hacky like:
    user_input = [line.rstrip() for line in sys.stdin.readlines()]
Which will give you:
    >> ["Spam", "Spam", "Spam"]

Sounds like an interesting ML problem, though dealing with a list of strings may complicate things significantly. You might want to make them one long string using something like join, e.g.

" ".join(user_input) # Used with a separator of " ", but you could use newlines or something

Which gives one string of:
    >> "Spam Spam Spam"
More on reddit.com
🌐 r/learnpython
10
13
June 15, 2019
multiline input in terminal - Ideas - Discussions on Python.org
Proposal: put simply, i need to regularly input multiple lines into my terminal and have python interpret that as is. for an oversimplified example: Enter your groceries: kiwi apple banana orange yes, i know there are workarounds such as while loops or sys.stdin.readlines() but those have cons ... More on discuss.python.org
🌐 discuss.python.org
0
November 29, 2024
Store multi-line input into a String (Python) - Stack Overflow
0 is there a way where i copy and paste a company address in python input and it prints as multiple line output? More on stackoverflow.com
🌐 stackoverflow.com
May 14, 2015
Reading multiple lines of input

What have you tried, and what form does the input take? If it's typed in, maybe you want something like this:

lines = []
line = input('First line: ')
while line:
    lines.append(line)
    line = input('Next line: ')
print(lines)

If you get the input from some source all at once, you can use

the_input.split('\n')
More on reddit.com
🌐 r/Python
5
0
March 6, 2015
🌐
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 ...
🌐
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'
              )
🌐
Python.org
discuss.python.org › ideas
multiline input in terminal - Ideas - Discussions on Python.org
November 29, 2024 - Proposal: put simply, i need to regularly input multiple lines into my terminal and have python interpret that as is. for an oversimplified example: Enter your groceries: kiwi apple banana orange yes, i know there are workarounds such as while loops or sys.stdin.readlines() but those have cons and dont exactly work as intended. for example, sys.stdin.readlines() would require me to only have one needed multiline input. in my opinion, input() should be for single lines while (something alon...
🌐
Delft Stack
delftstack.com › home › howto › python › python input multiple lines
Input Multiple Lines in Python
February 20, 2025 - The following code uses the raw_input() function to get multiline input from a user in Python. print "Enter your text (type 'stop' to end):" for line in iter(raw_input, 'stop'): print "You entered:", line
🌐
GeeksforGeeks
geeksforgeeks.org › python › taking-multiple-inputs-from-user-in-python
Taking multiple inputs from user in Python - GeeksforGeeks
2 weeks ago - 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....
Find elsewhere
🌐
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 ...
🌐
Quora
quora.com › How-can-I-take-multiline-input-from-a-user-and-assign-it-to-a-single-variable-in-Python
How to take multiline input from a user and assign it to a single variable in Python - Quora
Answer (1 of 4): First you need to figure out how the user indicates the end of the input. If it’s indicated in the last line (e.g. a blank last line indicates the end of input) you can read things in line-by-line: [code]user_writing = [] while True: line = input() if !line: # If line is blank...
🌐
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 - If you know in advance how many lines of input you need from the user, you can simply call input() multiple times and concatenate or collect these lines into a list or a single string.
🌐
CSEstack
csestack.org › home › how to read multiline user input in python 2 and 3?
How to Read Multiline User Input in Python 2 and 3?
April 15, 2019 - As sys module is present in both Python version 2 and 3, this code works for both Python versions. ... Enter your string in multiple lines. Once you complete giving the user input in multiple lines, press ctrl+d.
🌐
Medium
geekpython.medium.com › take-multiple-inputs-from-users-in-one-line-using-python-7c7594237f5a
Take Multiple Inputs From Users In One Line Using Python | by Sachin Pal | Medium
December 11, 2022 - If we use a simple approach like a beginner then we would write a Python program like below to take multiple inputs. x = input("Enter the input here: ") y = input("Enter the input here: ") z = input("Enter the input here: ") There are some ways through which we can restrict the above code to a single line for taking multiple inputs from the user in Python.
🌐
Reddit
reddit.com › r/python › reading multiple lines of input
r/Python on Reddit: Reading multiple lines of input
March 6, 2015 -

So I'm trying to get into regular programming practice. Spent some time doing challenges at CodeEval and the likes with C++ a little ways back, but now that I'm coming back I want to focus a little on Python.

The problem is, I can't figure out how to read the multiple line input test cases for my first program. In C++ it was easy with a while/get loop, but I'm struggling to figure it out in Python. It's frustrating because I know its simple and I've done it before, but I can't for the life of me figure out this simple problem that is holding me back from solving the bigger tasks.

So the test case for example would be:

SetCol 32 10

SetRow 20 4

QueryRow 10

etc, and I need to run my functions on each line of input.

Any advice as the best way to do this would be appreciated, so I can get back to practicing!

🌐
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?

🌐
Stack Overflow
stackoverflow.com › questions › 73329649 › how-to-get-input-from-user-in-python-with-multiple-lines
How to get input from user in python with multiple lines - Stack Overflow
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)
🌐
StudySmarter
studysmarter.co.uk › python multi input
Python Multi Input: Techniques & Examples | StudySmarter
Below is an example of collecting multiple lines of input until a specific termination word is input: inputs = []print('Please enter data line by line. Type · Python Multi Input: A crucial skill in Python enabling interactive and dynamic applications through simultaneous data input collection.
🌐
Real Python
realpython.com › lessons › getting-multiline-user-input
Getting Multiline User Input (Video) – Real Python
Getting Multiline User Input With Text Widgets. Text widgets are used for entering text just like Entry widgets. The difference is that text widgets may contain multiple lines of text. With a text widget, user can input a whole paragraph or even…
Published   May 21, 2024
🌐
Stack Overflow
stackoverflow.com › questions › 73601515 › how-to-take-either-multiple-or-single-input-on-one-line-from-python
How to take either multiple or single input on one line from Python - Stack Overflow
To accept multiple inputs on one line, I know you can do something like: a, b = input().split() And if the user were to only type 1 input, they would come across a ValueError: "ValueError: not
🌐
Medium
medium.com › @glasshost › multiple-lines-user-input-in-python-8644c0727467
Multiple Lines User Input in Python | by Glasshost | Medium
April 14, 2023 - In this example, we create an empty ... out each line of input. Another way to accept multiple lines of user input is by using a while loop....