Inputs and outputs are two important processes that allow programmer to let the user input some value to the program or to let the user get some output from the program.
For input processes, there is a function in Python named input().
For output processes, there is a function in Python named print().
input() function is responsible for getting some value as input from the console. Its syntax is as follows:
Example:
Here it must be kept in mind that the input() function brings in only a string input by default. So in order to let the user input data of other data types, we must invoke the input() function inside of the other functions like int() or float(). Consider the example:
Or:
print() function is responsible for printing some value as output to the console. Its syntax is as follows:
Here objects refer to the variables or values. sep is another argument that is used to separate objects. end is the argument that is used to specify the end of a line.
Printing objects: Objects can either be a variable or a constant value.
Example:
Similarly:
will run as:
We can use comma as default separator between multiple objects. Note that comma is replaced by a space in the output.
Example:
Here we see that a string and a variable are being printed together separated by comma. Its output will be:
Default separator is a comma that prints a space but we can specify the separator explicitly using sep argument.
Example:
Here we see that two objects are again being printed but not with a comma separator. They are separated by ‘--->’. Its output will be:
We use end argument to specify how to end the line. If we use any symbol as value of end argument, the line will end with that symbol. If that symbol is any escape sequence, the line will end that way.
Example:
Here we see that there is an end argument in the first print() statement. That is just to replace the default ‘\n’ character with the one that is specified in end argument. Its output will be: