Python Input and Output
Python has input() and print() function used for standard input and output operations respectively.
Output with print() function
The print()
function is used to output the data to the screen(standard output device or console).
Example using print() function
print('Print display message on screen')
Output
Print display message on screen
Another example of using print:
x = 5
print( 'The value of x is ', x)
Output
The value of x is 5
Example Using sep and end arguments of print() function
The syntax of the print()
function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
The argument, objects is the value(s) to be printed.
The argument sep separator is used between the values. The default value of sep is a space character.
end value is printed after all the values are printed The default value of end is new line.
The file is the object where the value(s ) is printed and its default value is sys.stdout (console or screen).
print('Print ','multiline', 'output to screen', sep='\n')
#Output
#multiline
#output to screen
print('Python', 'is', 'easy to', 'learn', sep=' ')
#Output Python is easy to learn
print('Python', 'is', 'easy to', 'learn', sep=',', end='#')
#Output Python,is,easy to,learn#
Output
Print
multiline
output to screen
Output Python is easy to learn
Output Python,is,easy to,learn#
Python Input
To accept the input from the user python has input() function to allow this. The syntax for function input() is:
input([prompt])
The prompt is the string you want to display on the screen. It is an optional argument.
name = input('Enter your name:')
Vijay
print(name)
#Output Vijay
Example reading an integer using input
#convert the string to integer using int() function
num = int(input('Enter a number:'))
Enter a number: 10
print(num)
#Output 10
Example reading a float value using input
#convert the string to integer using int() function
amount = int(input('Enter amount:'))
Enter amount: 1000.78
print(amount)
#Output 1000.78