Python: Input & OUTPUT Statements

While writing a program or develop any application need to required input from users on runtime by console (Keyboard) and after execution or debug the program required to show output on the console.

In the further section, you will learn how to enter user input on the running program and show output to the console.

input() Statement

Python provides input() built-in function to read the input from the console by using the standard input device (i.e. keyboard). Input function returns the string data irrespective of any datatype it is going to read as user input.

Syntax: variable_name = input([“user interactive statement”])

where,

variable_name is the variable assigned with the string value which is read using input method.

User Interactive statement is the statement displayed to the user expecting the response from them.

Example:

1.	input_var=input("please enter a number : ")
2.	print(input_var) 

Sample Output:

please enter a number: 100
100

print() Statement

Python provides print() built in function to print the output on to standard output device on console.

Syntax: print(“var_name1, var_name2, …”, [end=”value1”, sep=”value2”])

where,

var_name1, var_name2 are the variable names or the literals you want to print or output

end is used to specify the separator between two print statements which is ‘\n’ by default

sep is used to specify the separator between the different variables printed using print statement

Example:

	a="Saurabh"
	b=25.127
	c=20
	print(a,b,c)
	print(a,b,c,sep=":")
	print(a,b,c,end=" ")
	print(a,b,c)
	print("b=%0.2f" %b)
	print("c=%8d" %c)
	print("c=%-8d" %c) 

Sample Output:

Saurabh 20.127 10
Saurabh:20.127:10 #seperator between variables changed to ‘:’
Saurabh 20.127 10 infy 20.127 10 #seperator between two print statement changed to ” “
b=20.13 #as the format is 0.2 value is rounded of two decimal digits
c=10 #right aligned within the reserved 8 spaces 
c=10       #left aligned within the reserved 8 spaces as there is a – symbol

Through this Python input () and print() inbuilt function, you learn how to enter user input to run the program and print output statements in different formats.