sys.stdin.readlines() returns a list of lines. If what you want is one long (multi-line) string, use sys.stdin.read().

Answer from shx2 on Stack Overflow
🌐
Quora
quora.com › How-do-I-take-input-from-STDIN-in-Python
How to take input from STDIN in Python - Quora
Below are concise patterns and examples. ... Use input() to read one line as a string; strip() removes trailing newline and whitespace. ... Use split() to separate tokens; map() to convert types.
Discussions

python - How to use a string as stdin - Stack Overflow
I have been going crazy over a seemingly easy question with Python: I want to call a function that uses raw_input() and input(), and somehow supply those with a string in my program. I've been sear... More on stackoverflow.com
🌐 stackoverflow.com
How do I get sys.stdin.readline to read string
Try stripping the newline from input. User7 = str(sys.stdin.readline()).strip() Also, you need to compare with a string. That's what's causing your error. Python is looking for a variable named yes. if User7 == "yes": More on reddit.com
🌐 r/learnpython
3
2
December 12, 2022
python - How do I read from stdin? - Stack Overflow
Can simply rename to infile like ... do: docs.python.org/3/library/… 2018-12-23T21:52:53.703Z+00:00 ... Thank you @tommy.carstensen for your feedback, I have just improved the answer. Merry Christmas and Happy New Year ;-) 2018-12-27T21:20:56.953Z+00:00 ... The following chip of code will help you (it will read all of stdin blocking unto EOF, into one string)... More on stackoverflow.com
🌐 stackoverflow.com
python - Write function result to stdin - Stack Overflow
What I want to do is simulate keystrokes to the input() method by writing into stdin. ... Because I have a program that originally was in a shell and now it's using a GUI. What I want to do is convert click events into a string (done) and pipe the result to the previously used input() method. More on stackoverflow.com
🌐 stackoverflow.com
🌐
IncludeHelp
includehelp.com › python › how-do-you-read-from-stdin-in-python.aspx
How do you read from stdin in Python?
The input() method then reads a line from input, converts it to string (stripping a trailing newline), and returns that. test = input('Input any text here --> ') print("Input value is: ", test) ... Input any text here --> Hello Readers! Input value is: Hello Readers!
🌐
PythonHow
pythonhow.com › how › read-from-stdin-standard-input
Here is how to read from stdin (standard input) in Python
import sys input_text = sys.stdin.read() print("The input was:", input_text)You can also use file object to read from stdin in python like this: ... *Please note that input() returns the user input as a string, so if you want to use the input as a number, you'll need to convert it using int() or float().
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how do you read from stdin in python?
How do you read from stdin in Python? - Spark By {Examples}
May 31, 2024 - You will need to use a type conversion function like int() or float(). age = input("Enter your age: ") # The type of age will be string print(type(age)) # We need to convert it to other types age = int(age) print(f"age: {age}!")
🌐
Reddit
reddit.com › r/learnpython › how do i get sys.stdin.readline to read string
r/learnpython on Reddit: How do I get sys.stdin.readline to read string
December 12, 2022 -

my prof is making us use sys.stdout and sys.stdin instead of input and print, and I'm having trouble with my code. I'm new so yea

sys.stdout.write ("Enter yes or no: ")

User7=str(sys.stdin.readline())

if User7 == yes:

sys.stdout.write ("Stock code:22138\n")

It keeps saying yes not defined so I'm a bit puzzled on how to do that..

Find elsewhere
🌐
Stack Abuse
stackabuse.com › reading-from-stdin-in-python
Reading from stdin in Python
August 28, 2023 - $ python read_stdin.py Hello, world! Received: Hello, world! The input() function is a simpler way to read a line of input from stdin. This function reads a line of input, converts it to a string (stripping the trailing newline), and returns that string.
🌐
DigitalOcean
digitalocean.com › community › tutorials › read-stdin-python
How to Read from stdin in Python | DigitalOcean
August 3, 2022 - This function works in the same way as sys.stdin and adds a newline character to the end of the user-entered data. import fileinput for fileinput_line in fileinput.input(): if 'Exit' == fileinput_line.rstrip(): break print(f'Processing Message from fileinput.input() *****{fileinput_line}*****') print("Done") ... Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. ... Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean).
🌐
GitHub
gist.github.com › Tarrasch › 38fe96b02f87a2cfe45f30f7f7306e5e
Python program that read stdin and put it in array line by line · GitHub
Save Tarrasch/38fe96b02f87a2cfe45f30f7f7306e5e to your computer and use it in GitHub Desktop. Download ZIP · Python program that read stdin and put it in array line by line · Raw · read_stdin_lines.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
GeeksforGeeks
geeksforgeeks.org › difference-between-input-and-sys-stdin-readline
Difference between input() and sys.stdin.readline() - GeeksforGeeks
October 29, 2021 - How the input function works in Python : When input() function executes program flow will be stopped until the user has given input. The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, ...
🌐
CodeRivers
coderivers.org › blog › python-stdin
Python Standard Input (stdin): A Comprehensive Guide - CodeRivers
March 16, 2025 - The simplest way to read user input in Python is by using the built-in input() function. This function reads a line of input from stdin, converts it to a string, and returns the string value.
Top answer
1 of 5
30

You could mock stdin with a file-like object?

Copyimport sys
import StringIO

oldstdin = sys.stdin
sys.stdin = StringIO.StringIO('asdlkj')

print raw_input('.')       #  .asdlkj
2 of 5
2

I was googling how to do this myself and figured it out. For my situation I was taking some sample input from hackerrank.com and putting it in a file, then wanted to be able to use said file as my stdin, so that I could write a solution that could be easily copy/pasted into their IDE. I made my 2 python files executable, added the shebang. The first one reads my file and writes to stdout.

Copy#!/Users/ryandines/.local/share/virtualenvs/PythonPractice-U9gvG0nO/bin/python
# my_input.py
import sys

def read_input():
    lines = [line.rstrip('\n') for line in open('/Users/ryandines/Projects/PythonPractice/swfdump')]
    for my_line in lines:
        sys.stdout.write(my_line)
        sys.stdout.write("\n")

read_input()

The second file is the code I'm writing to solve a programming challenge. This was mine:

Copy#!/Users/ryandines/.local/share/virtualenvs/PythonPractice-U9gvG0nO/bin/python
def zip_stuff():

    n, x = map(int, input().split(' '))
    sheet = []

    for _ in range(x):
        sheet.append( map(float, input().split(' ')) )

    for i in zip(*sheet): 
        print( sum(i)/len(i) )

zip_stuff()

Then I use the operating system's pipe command to provide the buffering of STDIN. Works exactly like hackerrank.com, so I can easily cut/paste the sample input and also my corresponding code without changing anything. Call it like this: ./my_input.py | ./zip_stuff.py

🌐
Better Stack
betterstack.com › community › questions › how-to-read-stdin-in-python
How do I read from stdin in Python? | Better Stack Community
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.
🌐
CodeRivers
coderivers.org › blog › python-read-from-stdin
Python Read from STDIN: A Comprehensive Guide - CodeRivers
January 29, 2025 - The simplest way to read from STDIN in Python is by using the built-in input() function. This function halts the execution of the program and waits for the user to type some input and press Enter. The input is then returned as a string.
🌐
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 - The simplest and most commonly used method for reading input from stdin in Python is the input() function. This built-in function allows you to prompt the user for input, which can then be processed as needed.
🌐
UTK
web.eecs.utk.edu › ~bvanderz › teaching › cs365Sp19 › notes › Python › PythonIO.html
Utk
Console I/O: I generally don't recommend trying to get input from stdin using Python, but if you insist, read on. ... x = raw_input("Enter value: ") The input comes in as a string and you need to convert it to the appropriate representation using a cast.
🌐
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.