Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to use STDIN.Re
MohidMirza

Have you ever seen a python program that you can type into into and it responds? Once you know the trick it is really easy to do.
To begin, type:
import sys
Next, type this:
print(sys.stdin.readline())
If you type a few letters or words and press the ENTER key, you would get exactly what you typed.
To make this a variable, type
a = str(sys.stdin.readline())
Be careful, if you type a number, you will get an error. If you want the input to be a number, use this piece of code
a = int(sys.stdin.readline())
That's it!

Voters
MohidMirza
Comments
hotnewtop
DynamicSquid

To make this a variable, type a = str(sys.stdin.readline()). Be careful, if you type a number, you will get an error.

Actually, that's not true. You won't get an error if you type in a number. Also, you don't need to do str(), as readline() already returns a string.

MrVoo

Error on line 1, and what's the difference between that and int(input())?

DynamicSquid

@MrVoo There's not really a big difference. I think readline() includes the newline, and is faster when dealing with large input sizes. But I normally use input() since it's simpler