Input/Output in python with print() and input() functions.
A program or software without user interaction is nothing. In our last python tutorial, we have learned how to write a Hello, World program. The next task is to learn how we can make python software interactive. For that, we will learn about the input function, to get data from the user. And another routing print, to display output on standard output.
How to use the Input() function to read from standard input – Keyboard?
Syntax or prototype – return_value input([prompt])
Similar to a function in any other programming language, the input function takes an argument and returns a value. It takes only a single optional argument (A message string to display) from standard input e.g keyboard and returns the entered value as a string.
How does the input function work?
While execution when control reaches the function, program display [prompt], if any, and start waiting for user input. Once the user enters a value and press enter, the control moves next, and the entered value is stored in a return variable. The function reads the full line entered from the user other than a newline character in the end.
Example to get input from the user via keyboard in Python:
- This is a very basic example program.
- On execution, it takes input from the keyboard.
- Store input in a variable (we will describe variables in Python in another tutorial, for now, assume it as a container with a name in which python stores a value).
- Display the entered value.
# Python Program to read input from user. dayName = input("Enter the Day Today "); print("Today is ",dayName)
Enter the Day Today Monday Today is Monday
Example for adding two numbers.
Programs are for performing a task. The task may be as simple to add two numbers or it can be very complex business logic. In both cases, there are input parameters and an outcome. The following program shows a basic addition of two numbers.
- Programs prompt to enter the first number.
- User Enters a number.
- Prompts for the second number. The user enters the second number.
- Stores both values in two variables.
- Coverts entered values into integers ( as the entered values are strings. The addition applies to numbers).
- Do the sum of two numbers and store them in another variable.
- Displays the result of the sum.
# Python Program to two numbers and display #the SUM. first = input("Enter the first number "); second = input("Enter the second number "); sum = int(first) + int(second); print("Result = ",sum);
Enter the first number 3 Enter the second number 4 Result = 7
In a similar way, you can add two floating-point (decimal) numbers too. In that case, the conversion will be from string to float using the float() function. There can be several ways to enter inputs. Just keep in mind whatever you enter is a string. All string operators/operators are applicable.
Learn Output using print() function in python:
Although we have already used the print function for display. But in this section, we will cover more details about the output. An output may be fancier and meaning full using various options. The print function writes the output to the standard output, so we can see it on screen.
Prototype – print(objectslist, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
The function takes five arguments and returns nothing. All arguments are optional. If the function call is with no argument, it simply prints a new line. Here is the description of each argument.
objectslist, – The first argument. The argument itself contains multiple values to print. In python everything is an object, so we calling this an object list. E.g “String”, variable1, “text” etc. Can we very long.
sep – It is for a pattern that will separate the object while displaying. By default, a single space is a separator.
end – Will be displayed at the end of the object list. By default, it is a new line.
file – Where the output will be written. By default, it is on standard output (console).
flush – It is a boolean value. Whether there is buffering or not after print. The default value is false.
Examples to print objects with no options, with separators, with two new lines in the end and on standard output.
#Print examples #two objects with not option. print("Paul", "John"); #two objects separated by : print("Paul", "John",sep=':'); print("Paul", "John",sep='||', end='\n\n'); print("Program Ends");
Paul John Paul:John Paul||John Program Ends
- In the first line, strings are separated by a space (default separator), and the next print in the next line (the end is a new line by default).
- In the second line, string objects are separated by : (
- In the third line, || separates the objects and the next output is after two lines [
How to write output other than standard output?
Till now we see all output on the screen, but what if we want to write somewhere else? It is not always possible to monitor the screen all the time. The jobs may run in the background and saves the result in a file. Later a person can check the outcome.
Here we will demonstrate a python program, that will write the output in a file.
- Open a file in write mode (more options will cover later in file input/output).
- Write two objects in the file.
#Print examples to write in a file #two objects with not option. outfile = open('output.txt', 'w') print("Paul", "John", file = outfile);
After the execution of the python script, we can see that nothing is on the console and a file is created in the same folder where the script is present. If you open the file you will see “Paul John”.