So, took your code out of the function and ran some tests.

import sys
buffer = []
while run:
    line = sys.stdin.readline().rstrip('\n')
    if line == 'quit':
        run = False
    else:
        buffer.append(line)

print buffer

Changes:

  • Removed the 'for' loop
  • Using 'readline' instead of 'readlines'
  • strip'd out the '\n' after input, so all processing afterwards is much easier.

Another way:

import sys
buffer = []
while True:
    line = sys.stdin.readline().rstrip('\n')
    if line == 'quit':
        break
    else:
        buffer.append(line)
print buffer

Takes out the 'run' variable, as it is not really needed.

Answer from Wing Tang Wong on Stack Overflow
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ reading-from-stdin-in-python
Reading from stdin in Python
August 28, 2023 - In this code, each line that is ... by pressing Ctrl+D in the terminal. Another way to read multiple lines of input from stdin is by using the input() function inside a loop....
Discussions

How to read line by line from stdin in python - Stack Overflow
Everyone knows how to count the characters from STDIN in C. However, when I tried to do that in python3, I find it is a puzzle. (counter.py) import sys chrCounter = 0 for line in sys.stdin.readline(): chrCounter += len(line) print(chrCounter) More on stackoverflow.com
๐ŸŒ stackoverflow.com
python 3.x - Python3 : Using input() how to Read Multiple Lines from pipe (stdin)? - Stack Overflow
Lets say you wanted to take the second line and create an array: stdin: ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... New site design and philosophy for Stack Overflow: Starting February 24, 2026... 34 How do you read from stdin in python ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How do I read from stdin? - Stack Overflow
Python also has built-in functions ... See the Python documentation under Built-in Functions. ... This reads a single line, which isn't really what the OP asked about. I interpret the question as "how do I read a bunch of lines from an open file handle until EOF?" 2015-12-22T08:51:49.37Z+00:00 ... The OP isn't asking to read input from a keyboard, He is asking to read from stdin which in a ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Using sys.stdin.readline() to read multiple lines from cmd in Python - Stack Overflow
Basically, if you want multiline ... sys.stdin is a file-like object in Python, the read() method will read until it reaches the end of a file. It is marked by a special character EOF (end-of-file). On different OS'es there is a different way of sending it. On Windows: Press Ctrl+Z after your input and then press Enter: ... On a Unix-based OS: Press Ctrl+D after your input. No Enter is required (I believe) If you want to get a list [2, 10, 20, 2, 30, 3] from your input, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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 โ€บ 31816239 โ€บ python3-using-input-how-to-read-multiple-lines-from-pipe-stdin
python 3.x - Python3 : Using input() how to Read Multiple Lines from pipe (stdin)? - Stack Overflow
Note that it won't read until there is an EOF terminating stdin. ... Sign up to request clarification or add additional context in comments. ... thanks. but that isnt an answer to my question. its just more or less what i already did up there. - i would like to know, is there a way to use the plain old "input()" to achieve pretty much the same without blocking the programm and to read more than just one line from the pipe.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ read user input (stdin) in python
Read user input (STDIN) in Python | Sentry
November 15, 2023 - Python provides a few methods for reading from stdin in different contexts. The simplest and most commonly used is the default input function, which prompts the user to enter a line into stdin and returns it as a string once they press Enter.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Read MULTIPLE lines from standard input in Python ๐Ÿ #shorts - YouTube
Use sys.stdin to read over multiple lines in standard input in Python. Useful for when you are piping data from one command into a Python program.
Published ย  December 31, 2022
Find elsewhere
๐ŸŒ
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 - Once you complete giving the user input in multiple lines, press ctrl+d. It sends signalEOF to your system. If you are a windows user, use ctrl+z instead of ctrl+d. And enter.โ€ ยท But still my console is allowing to enter from keyboards .. why ??
๐ŸŒ
Better Stack
betterstack.com โ€บ community โ€บ questions โ€บ how-to-read-stdin-in-python
How do I read from stdin in Python? | Better Stack Community
October 5, 2023 - In Python, you can read from standard input (stdin) using the input() function. This function blocks execution and waits for the user to enter some text, which is then returned as a string.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python input multiple lines
Input Multiple Lines in Python
February 20, 2025 - The sys.stdin.readlines() method is a part of the sys module and is designed to read all lines from the standard input until an EOF (End Of File) is reached.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ how to read input from stdin in python
How to Read Input From stdin in Python | Delft Stack
March 11, 2025 - Python provides a straightforward way to handle file input using the open() function in conjunction with stdin. This is particularly useful for processing large datasets or configuration files.
๐ŸŒ
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!

๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-input-multiple-lines
Multiple lines user Input in Python | bobbyhadz
Copied!import sys # ๐Ÿ‘‡๏ธ User must press Ctrl + D (Unix) or Ctrl + Z (Windows) to exit print('Press CTRL + D (Unix) or CTRL + Z (Windows) to exit') user_input = sys.stdin.readlines() # ๐Ÿ‘‡๏ธ get list of lines print(user_input) # ๐Ÿ‘‡๏ธ join the list items into a string print(''.join(user_input))
๐ŸŒ
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...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 327267 โ€บ can-you-read-multiple-lines-from-stdin
Can you read multiple lines from stdin? | Sololearn: Learn to code for FREE!
First, I think that you should ... also allocate dynamically Then you can simply read in the lines: for (int i = 0; i < 10; ++i) fgets(lines [i], 10, stdin); This is a very simple code that reads you some lines....
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2881136 โ€บ how-to-get-multi-line-input-from-user
How to get multi-line input from user? | Sololearn: Learn to code for FREE!
September 13, 2021 - 1st Method: inputlist = [] while True: try: line = input() except EOFError: break inputlist.append(line) 2nd Method import sys inputlist = sys.stdin.readlines() print(inputlist) This will take multi-line input however you need to terminate the ...
๐ŸŒ
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
docs.python.org โ€บ 3.3 โ€บ library โ€บ fileinput.html
11.2. fileinput โ€” Iterate over lines from multiple input streams โ€” Python 3.3.7 documentation
using sys.stdin.seek(0)). Empty files are opened and immediately closed; the only time their presence in the list of filenames is noticeable at all is when the last file opened is empty. Lines are returned with any newlines intact, which means that the last line in a file may not have one.