Input in Python: To make a Software or Program interactive, the first and most important step is to add the functionality of taking input from the user.
This helps in keeping the user engaged and is also a much realistic approach to create an application.
Every programming language has its method of taking input from a user. Similarly, Python also has a built-in method using which you can easily take input from the user.
In this article, we are going to take a deep look at Input functionality in Python.
So, let’s get started with the syntax of the Input method in Python.
Table of Contents
Syntax of Input in Python
To take the data from the user during Runtime, we make use of the input() method in Python.
Here is the basic syntax of input() in Python.
input(prompt_message)
So, whenever the Python compiler gets the input() method during the execution of the program.
The program gets stopped and waits for the user to enter something(Text or Number).
After the user enters the value, the flow of the program again gets started from the point where it is left.
The prompt_message parameter is used to display the message to the user before entering the string or value.
Please note that the prompt_message parameter is optional. So, you don’t have to use it, if it is not necessary.
So, don’t worry, in case you didn’t understand anything. Because in the following article, we are going to take a deep look into the working of input() with examples.
So, let’s start with the examples of user input in python with as well as without prompt message.
Getting User Input in Python
In Python, you can take input from the user with the help of the input() method.
For example –
input()
Output:
>> CoderPedia
However, to store this input in a variable, we need to use the following code.
var = input()
print(var)
Output:
>> CoderPedia
Coderpedia
As you can see in the above code, we have equated the input() with the variable name “var”.
As a result, the “var” variable has stored, whatever we have entered using the input() method. In our case, we have entered “CoderPedia”.
In the second line, we have printed the message inside the “var” variable using print().
This was the example of the input() method without a prompt message.
Now, let’s take a look at the input() method with a prompt message.
txt = input("Enter a Text: ")
print(txt)
>> Enter a Text: Welcome to CoderPedia
Welcome to CoderPedia
As you can see in the above program, the prompt message is “Enter a Text: “.
This prompt message is used to give the idea to user about what to enter.
As in this program, we are asking the user to enter the text value.
This code is similar to the below code, in which we have used the print() method to tell the user about what to enter as a input.
print("Enter a Text: ")
txt = input()
print(txt)
Enter a Text:
>> Welcome to CoderPedia
Welcome to CoderPedia
Return Value from Input and Input Types
One of the most important points to keep in mind while using the input() method is that the input() only returns the string value.
So, it doesn’t matter whether you enter the string value, integer value, or float value. You will only get the string as a return type.
Let’s see this with the help of example.
txt = input("Enter a String: ")
print("txt type is ", type(txt))
num = input("Enter a Number: ")
print("num type is ", type(num))
floatNum = input("Enter a Floating Point Numner: ")
print("floatNum type is ", type(floatNum))
>> Enter a String: Visit CoderPedia
str type is <class ‘str’>
>> Enter a Number: 34
num type is <class ‘str’>
>> Enter a Floating Point Number: 32.61
floatNum type is <class ‘str’>
As you can see in the above code, the input() method returns the string value only.
So, in case we enter the integer or float value and want to perform any type of operation (like – addition or multiplication) in it. Then we will get an error.
To resolve this issue, we are going to make use of type conversion, which we have discussed below.
Best Course
-> Best Course for learning Python: Python for Everybody Specialization
Getting Integer as a User Input
As we have already seen that the input() only returns a string value.
So, we have to make use of type conversion, to convert the string to int or string to a floating-point number.
Let’s see the example of getting an integer as user input.
num = input("Enter a Number: ")
iNumber = int(num)
print("Square of Number is ", iNumber * iNumber)
>> Enter a Number: 32
Square of Number is 1024
As you can see in the above code that we have first asked the user to enter the number.
However, the number is of string type. So, in the second line, we have used int() (integer type conversion) method to convert the string to int.
As a result, we have successfully performed the multiplication in the third line.
In the below code, we have used another way of performing the type conversion, which is relatively shorter and simple.
num = int(input("Enter a Number: "))
print("Square of Number is ", num * num)
>> Enter a Number: 64
Square of Number is 4096
As you can see that, In the above code, we have directly converted the input to an integer value, which is also a smarter approach to performing a type conversion.
Getting Floating Point Number as a User Input
Similarly, we can perform floating-point typecasting, to get the floating point number as a user input.
floatNum = float(input("Enter a Floating Point Number: "))
print("Double of Floating Point Number is ", floatNum + floatNum)
>> Enter a Floating Point Number: 45.67
Double of Floating Point Number is 91.34
As you can see in the above program that we have converted the input into the floating-point number using the float() method.
As a result, we have successfully performed the addition operation in it.
Conclusion
Hope you like the in-depth article on Input in Python, where we have discussed various useful topic like -Syntax, Getting User Input, Return value from input(), Getting Integer as a User Input and at last, Getting Floating Point Number as a User Input.
We also have a article on Python Print without Newline, which you must take a look at.