What's the difference between input() and sys.stdin.readline()?

basically that is my question hahahahah
I create a function and test both, apparently is the same thing. would like to know if it really is the same or is just my lack of knowledge on the subject.

My code:

import sys
#3 moon weight program
#using sys.stdin.readline() mini-program using prompt

def moon_weight_sys():
#print("What is your weight on earth? ")
#weight_earth = int(sys.stdin.readline())
weight_earth = int(input("What is your weight on earth? "))
#print("How munch do you gain in weight each year? ")
#weight_increased = int(sys.stdin.readline())
weight_increased = int(input("How munch do you gain in weight each year? "))
#print("In how many years? ")
#years_num = int(sys.stdin.readline()) + 1
years_num = int(input("In how many years? ")) + 1
moon_weight_now = weight_earth * 0.165
for y in range(1, years_num):
print(“Year {}: My weight on the moon is {}.”.format(y, moon_weight_now))
weight_earth = weight_earth + weight_increased
moon_weight_now = weight_earth * 0.165

moon_weight_sys()

Basically, yes.
The main difference is that when you use the input and print function, all the output formatting job is done behind the scenes.

stdin is used for all interactive input, including calls to input();
stdout is used for the output of print() and expression statements and for the prompts of input()

Go to the Python Interpreter and play with the following code:

import sys

first_input = sys.stdin.readline() # Then type Hello World (or whatever your want)!

first_input # The expected output will be ‘Hello World!\n

second_input = input() # Type Hello World

second_input # It will be equal to ‘Hello World!’, but this time, there is no escape sequence

sys.stdout.write(first_input) # Try this now and see the result. Then check if it will be the same for print(first_input)

print(first_input)

Here are some links in case you want to go more deeper.

https://docs.python.org/3/tutorial/inputoutput.html?highlight=input

https://docs.python.org/3/library/sys.html

https://docs.python.org/3/library/functions.html#print

1 Like

What @funception wrote works, but remember that this is different for Python 3 and Python 2. In Python 2 input() will attempt to convert the input element to something Python, which then becomes a security flaw (since it executes code you type). In Python 3 they removed that “feature”.