Standard Input & Output in Python

 Standard Input & Output in Python 

The standard Input and Output in python means the default ways of receiving input to a program and dispalying result from a program.

Standard Input (stdin): The default source of input data (usually the keyboard).
Standard Output (stdout): The default destination for output data (usually the console/screen).

When an user types on the Keyboard, the program takes it as input to python program using a predefined method input().

The Program always  receives the data through input() function in String Format. Let's Understand the below program.


name = input("Enter your name : ")
print(name)


Output : 

Enter your name : python python

In the above program, name is a variable in which the entered name 'python' is getting stored. As soon as user Press the Enter Key, the interpretor process the input and displays the output.

Hence, print() is the standard output function which displays the output to the console/terminal window.


Advanced input and output in python :


For more advanced input and output, we use sys.stdin and sys.stdout from sys module.

Example

import sys
line = sys.stdin.readline
print(line)


Ouput :

Python Programming

Python Programming

Example


import sys
sys.stdout("Hello World!")


Output : 

Hello World!

 

Summary Table

Operation             Function/Method                 Module                 Example

Read input             input()                                         built-in                 input("Prompt: ")

Read input             sys.stdin                                 sys                         for line in sys.stdin:

Write output             print()                                         built-in                 print("Hello")

Write output             sys.stdout                                 sys                         sys.stdout.write("Hi\n")

Note:

input() always returns a string.

print() adds a newline by default; use end=' ' to change this.

sys.stdin and sys.stdout are for advanced use.



Prevous Topic                                                                                                      Next Topic

Comments