Python Input | How to Take Input in Python from user

While programming, several times you might have to take input from the users and then perform operations on that input to get your output. In Python, there are several ways in which you can take this input from the user and these Python input taking methods are discussed below.

Taking Input from the User in Python

There are two inbuilt functions that will let you take input from the user directly. They are raw_input ( ) and input ( )

a) raw_input ( )

raw_input () takes exactly what the user has typed and passes it back as a string. It doesn’t interpret the type of input entered by the user. This means, even when the user inputs an integer value, it will still be considered as a string. This inbuilt function can only be used in Python 2.x & not in Python 3.x.

Here is an example implementation of raw_input in Python 2.x.

#Python program using the raw input function
a = raw_input (" Enter your favorite website: ")
print(a)
Input given by the user: FACE Prep

Output: 
Enter your favorite website: FACE Prep

In this above code, let us now include one additional statement using type () function to find out the type of data entered by the user. Also, this time let us input an integer and check the result.

#Python program using the raw input function
a = raw_input (" Enter your favorite website: ")
print(a)
print type(a)
Input given by the user: 567

Output: 
Enter your favorite website: FACE Prep
<type ‘str’>

Explanation: This clearly shows that irrespective of the type of data inputted by the user (be it an integer, float, character, or string), the raw_input() function always returns a string.

b) input ( )

input () interprets and evaluates the input entered by the user, which means if the user enters an integer, an integer will be returned and if the user enters a string, a string will be returned. This is how it works in Python 2.x versions. Whereas in Python 3.x versions, input () always returns a string and this function is just a replacement on raw_input () function.

Here is an example to understand the input function.
In Python 2
#Python program using input function
a = input ("Enter a webiste name: ")
print a
print type(a)

b = input ("Enter your lucky number: ")
print b
print type(b)

c = input ("Enter a random decimal number: ")
print c
print type(c)
Input from the user: 
"FACE Prep"
7
89.03


Output: 
Enter a website name: FACE Prep
<type 'str'>
Enter your lucky number: 7
<type 'int'>
Enter a random decimal number: 89.03
<type 'float'>
Explanation: If you observe, here the return type is exactly the same as the type of data entered by the user. Also, note that whenever you input a string in Python 2.x through input function, make sure you put in double-quotes, else the compiler throws a name error as it considers your string to be a variable.
In Python 3
#Python program using input function
a = input ("Enter a webiste name: ")
print (a)
print (type(a))

b = input ("Enter your lucky number: ")
print (b)
print (type(b))

c = input ("Enter a random decimal number: ")
print (c)
print (type(c))
Input from the user: 
FACE Prep
7
89.03
Output: Enter a website name: FACE Prep <type ‘str’> Enter your lucky number: 7 <type ‘str’> Enter a random decimal number: 89.03 <type ‘str’>
Explanation: Since Python input function performs the job of raw_input function, it returns all the entered data as string only.

Python Input FAQs

1) Which among input () and raw input () should I use?

It’s pretty simple. Python 2 is now outdated and hence there is no point in planning to continue using it. Whereas coming to Python 3, the raw_input function is renamed as input and this is the only function available for use. So you will have to use input () to take input from the users in Python 3.

2) What is the difference between raw_input and input?

In Python 2, raw_input() function always evaluates and the data type of the entered input as String, no matter what data is entered. Whereas the function input() always evaluates and return the exact data type of data entered by user.

In Python 3, raw_input doesn’t exist and the existing input() performs the functions of raw_input().

3) How does the input function work in Python?

The Python compiler reads the code from top to bottom. So whenever it finds an input function written this way input(“prompt”), it stops and displays the prompt message. Then, it waits for the user to enter the input value and then proceeds.

4) How do I read my input as an integer?

There is no way we can stop Python input () function from converting the inputted data to a string. However, you can reconvert the inputted data from string to the intended data type. To do this you need to be aware of the concept of typecasting. Click here to check it out.

5) How to take an integer input in Python?

Using the above-discussed input() function, you will be able to take an integer input from the user. But, you will not be able to perform certain operations on this inputted data. This is because, in Python 3, every input given by the user is converted into a string data type.

Now, say for example, you need to add 10 to the inputted integer, then Python will throw an error. This is because although the user entered an integer value, its return type is now string and technically you are trying to add this 10 to a string. Thus you need to first convert the input to an integer type and then add 10. 

Here are some examples:

#Taking integer inputs from the user 
a = input ("Enter a number: ")
print(a)
print (type(a))

b = a+10
print(b)
print (type(b))

Output: 

Enter a number: 7
<class 'str'>
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    b = a+10
TypeError: must be str, not int

6) How to take a string input from the user?

This is pretty straightforward in Python 3 and the syntax for this is:

a = input (" Enter the prompt ")
print (a)