| by Arround The Web | No comments

Python Input() Function

Python comes with a built-in function named “input()” that accepts the user input. It typically returns the user input as a string by default.

The syntax to employ this method is given in the following:

It has only one parameter “prompt” which is a string message that asks the user to input the value.

We will execute various example programs to implement this method into practice in this article.

Example 1: Utilizing the Input() Method to Take a Single Input from the User

We commence our article by implementing a simple Python example that takes a single input from the user using the Python “input()” method and then simply display the provided value on the screen.

In the code provided, we first create a variable “data” and assign it the output value which is generated from calling the “input()” function. In between the round braces of the “input()” method, we define a prompt argument that asks the user to “Please Enter a value:”. This prompt argument is optional. You can even invoke the “input()” function without using it but we would prefer to add it so that the user can see what he/she is required to input.

When the control executes the “input()” method, the program stops, showing a “Please Enter a value:” prompt on the terminal and waits for the user to insert the value. Using the keyboard, the user adds any value and then presses “Enter”. The “input()” function reads this value and then converts it into a string (this method converts any type of provided value into a string by default), then ultimately return this value to the program.

The “print()” method is invoked in the preceding line of code. The Python “print()” method displays whatever is provided as input to it. We first provide a string to display, i.e. “The user provided value is:”. Then, the variable “data” is supplied to display the content it stores.

This image shows a prompt on the terminal when the “input()” method is executed. A message is displayed which requests the user to input a value. The user enters a value and presses Enter.

The second output image exhibits the value that the user has provided which is “24”. Then, the program prints this value with the specified statement on the terminal.

Example 2: Utilizing the Input() Method to Take Multiple Inputs from the User

We learned to take a single value input from the user. Moreover, multiple inputs can also be requested from the user by employing the “input()” function. To implement this concept, we created the following example code:

In this illustration, we take 4 input values from the user. We created four variables as “st_id”, “subject”, “marks”, and “grade”. We assigned each variable the outcome of calling the “input()” method. For the first variable “st_id”, the “input()” function displays the “Enter Student Id:” prompt. The program is paused while it waits for the user to enter the student’s ID.

After reading the student id provided by the user, it stores it in the “st_id” variable and moves to the next line of the code. The next line executes the “input()” method and a prompt is exhibited as “Enter Subject title:” on the terminal. The user has to insert the subject title and the program saves it into the “subject” variable. It then moves forward to execute the remaining two “input()” functions one by one.

After executing all the “input()” methods, the next line contains a “print()” function to move to the next line where another “print()” function has a statement – “Displaying Student’s Details”. Then, we put into view all the input values taken from the user respectively using the “print()” functions.

Here, you can see that the program executes the first “input()” method and the prompt asks the user to input the Student Id.

As soon as the user hits Enter after inserting the student id, another prompt is displayed that requires the user to provide the subject title.

The next “input()” method executes and asks the user to input the marks.

Lastly, the user has to insert the grade.  After pressing “Enter”, all the details that the user has provided are displayed on the terminal.

Whether you provide an integer value or any other, all these values are converted into string datatype by the default behavior of the “input()” method. To verify the datatype of all the input provided in the previous snapshots, we have the “type()” method in Python.

We embedded the “type()” method with the “print()” method. Between the braces of the “type()” method, we have to supply the variable whose datatype are needed to verify.  Each variable is provided one by one in the “type()” function.

This output image shows the datatype for each provided value. We can see that class is mentioned as “str” for all the values. This means that the “input()” function converted the datatypes for all the values to strings.

Example 3: Utilizing the Input() Method to Take the Integer Value Input from the User

We have come to learn from the previous instances that the “input()” function changes the datatype of any value to the string type. Conditions might exist in which you would require keeping the provided datatype. To do this, we have to explicitly provide the datatype before the “input()” function. The following is a sample program that we wrote to multiply two integer numbers:

In this demonstration, we have a variable “Value_1” which stores the first value. To take the input value from the user, the “input()” method is invoked. But before this method, we also cast the “int” datatype. So, when the user provides the value, the “input()” method converts the value into a string type. Then, the datatype that we cast on the “input()” method which is “int” changes the datatype to an integer.

In the same way, we take in the second value and store it in the variable “Value_2”. Then, we print both values using Python’s “print()” method. Lastly, we create a script to calculate the multiplication of “Value_1” and “Value_2”. And store the outcome in the “multiplication” variable. The multiplication result is exhibited using the “print()” method.

The previous snapshot shows that the user has entered two values – 12 and 3. Then, it displays both the values as the “First Value” and the “Second Value”. Finally, after multiplying the first and second values, it exhibits the result which is “36”.

Now, to verify the data type of the resultant variable, we employed Python’s “type()” function.

Here, we can examine that the type is mentioned as “int”.


For this tutorial, we learned to take the user input values using Python’s “input()” method. This writing covered the simplest demonstration to take a single value from the user where a prompt is displayed on the terminal and the user enters the value. Then, we elaborated on the use of the “input()” method to input more than one value. Also, verifying the datatype of the values is done using the “type()” method. Lastly, we demonstrated the use of the datatype casting to change the value type from string to a required one. Collectively, all these instances make the concept comprehensive.

Share Button

Source: linuxhint.com

Leave a Reply