explain the role of input () and print() function in python with suitable example for each
Answers
Explanation:
Python provides numerous built-in functions that are readily available to us at the Python prompt.
Some of the functions like input() and print() are widely used for standard input and output operations respectively. Let us see the output section first.
Python Output Using print() function
We use the print() function to output data to the standard output device (screen). We can also output data to a file, but this will be discussed later.
An example of its use is given below.
print('This sentence is output to the screen')
Output
This sentence is output to the screen
Another example is given below:
a = 5 print('The value of a is', a)
Output
The value of a is 5
In the second print() statement, we can notice that space was added between the string and the value of variable a. This is by default, but we can change it.
The actual syntax of the print() function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Here, objects is the value(s) to be printed.
The sep separator is used between the values. It defaults into a space character.
After all values are printed, end is printed. It defaults into a new line.
The file is the object where the values are printed and its default value is sys.stdout (screen). Here is an example to illustrate this.