Sunday, February 23, 2020

Input Output In Python

Basic Input Output Operations In Python


input() and print() are the basic functions available for standard input-output operations.

The input() method

The input() method is equivalent to the raw_input() method in Python 2.7. The program waits for the user input, and redeem itself when some input is provided by the user.

The print method allows user input form the keyboard.

The syntax of the input() is,

input(prompt), where prompt is the default message before the input.


name=input('Enter username')
print(name)

#Enter username programminghunk
#programminghunk



OR

Taking the number as input

a=int(input('Enter A'))
b=int(input('Enter B'))
print(a+b)

#Enter A 12
#Enter B 23
#35

The print() method

The print() method is used to print the output on the console. In Python 3, the print() is a function and works like

print("This message will be displayed in the output")
# This message will be displayed in the output
print("we can print \nescape characters too.")
# we can print escape characters too.


In Python 2, the print was originally a statement, for example.

print "This message will be displayed in the output"
# This message will be displayed in the output
print "we can print \nescape characters too."
# we can print escape characters too.


The complete syntax of the print() method is
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

The object represents the objects to be printed. Here Symbol(*) represents that there can be multiple objects to print.

The sep is a separator used to separate the objects. This is optional.

The end represents which object should print at last(by default '\n'). This is also optional.

A file is an object to write with. (by default sys.stdout writes to the console).

The flush represents that if the stream should be forcefully flushed or not. Its default value is False.