×

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below:

  • raw_input()
  • input()

What is raw_input() Function?

raw_input() function is a built-in function in Python. It is used to get the values or to receive from the user.

The return type of the raw_input() function is in the form of string type or STR. This input function is applicable for or used only in Python 2. x version.

Example:

Let’s understand it by taking an example is below:

# raw_input() function in Python 2.x


x = raw_input("enter 1st input: ")
y = raw_input("enter 2nd input: ")
z = raw_input("enter 3rd input: ")


# print the value of the input function
print "value of 1st input: ", x


#print the type of input value
print "type of 1st input: ", type(x)


print "value of 2nd input: ", y
print "type of 2nd input: ", type(y)
print "value of 3rd input: ", z
print "type of 3rd input: ", type(z)

Input:

5

"javatpoint"

6.79

Output:

value of 1st input:  5
type of 1st input:  <type 'str'>
value of 2nd input:  "javatpoint"
type of 2nd input:  <type 'str'>
value of 3rd input:  6.79
type of 3rd input:  <type 'str'>

This is the example of the raw_input() function in Python 2. x. In this, the return type of this function is only string type. Because of this, if we enter the value 5, then the return type is string only, so if we want to convert it into an integer, then we have to type cast it. And in the above example, we give input as 6.79, and then the return type is string only, so if we want to convert it into a float, then we have to type cast it.

In this, we are required to perform typecasting explicitly.

Example

Let’s understand typecasting by taking the example is below:

Example by using typecasting:

# raw_input() function in Python 2.x
# typecasting in raw_input() function
x = int(raw_input("enter 1st input: "))
y = raw_input("enter 2nd input: ")
z = float(raw_input("enter 3rd input: "))
print "value of 1st input: ", x
print "type of 1st input: ", type(x)


print "value of 2nd input: ", y
print "type of 2nd input: ", type(y)


print "value of 3rd input: ", z
print "type of 3rd input: ", type(z)

Input:

5

"javatpoint"

6.79

Output:

value of 1st input:  5
type of 1st input:  <type 'int'>
value of 2nd input:  "javatpoint"
type of 2nd input:  <type 'str'>
value of 3rd input:  6.79
type of 3rd input:  <type 'float'>

The typecasting return type of 5 is 'int', and the return type of 6.79 is 'float'.

What is Input() Function?

The input() function takes values from the user and informs the programme to wait for the user to enter the values. It is a built-in function in Python. This input function is applicable for or used in Python 2. x and 3. x versions.

Let’s first discuss about input() function in Python 2 .x .

In Python 2. x, the input() function takes the value from the user, and the return type is based on the user's provided value (if you enter the value as an int type, then the return type itself will be 'int’).

Example:

# input() function in Python 2.x
x = input("enter 1st input: ")
y = input("enter 2nd input: ")
z = input("enter 3rd input: ")


#print the value of input value
print "value of 1st input: ", x
print "type of 1st input: ", type(x)


print "value of 2nd input: ", y
print "type of 2nd input: ", type(y)


print "value of 3rd input: ",z
print "type of 3rd input: ", type(z)

Input:

5

"javatpoint"

6.79

Output:

value of 1st input:  5
type of 1st input:  <type 'int'>
value of 2nd input:  javatpoint
type of 2nd input:  <type 'str'>
value of 3rd input:  6.79
type of 3rd input:  <type 'float'

In this, we are not required to perform typecasting explicitly.

The drawback of using input function in python 2. x:

As we know, in python 2. x, we can also input a variable using the input function. This makes our program less secure. To understand it, let's take an example.

Suppose we make a log-in page and its back-end in python 2. x, for log-in user inputs his user_name and password.

user_name = "test_name@231"
password = "test_password231"


name_input = input("Enter username: ")
password_input = input("Enter password: ")


# if user_name is equal to name_input and 
#password is equal to password_input. Only then can the user log in.


if user_name == name_input and password == password_input:
print "logged in successfully"
else:
print "wrong user name or password"

At user’s side:

>> Enter username: "name@453" 
>> Enter password: "3@stak*s"
>> wrong user name or password

It seems everything is working fine for invalid user names and password users cannot log in. But if the user inputs user_name and password.

At user’s side:

>> Enter username: user_name
>> Enter password: password
>> logged in successfully

Here, a user can log in without a correct username and password. So the input function is less secure. If you are using the python 2. x version, it is not recommended to use the input function and the raw_input function that returns string function and typecast if needed.

let’s see above example with raw_input finction
user_name = "test_name@231"
password = "test_password231"


name_input = raw_input("Enter username: ")
password_input = raw_input("Enter password: ")


# if user_name is equal to name_input and 
#password is equal to password_input. Only then the user can log in.


if user_name == name_input and password == password_input:
print "logged in successfully"
else:
print "wrong user name or password"

At user’s side:

>> Enter username: user_name
>> Enter password: password
>> wrong user name or password

Here you can see that users cannot log in after giving input user_name and password, so it is more secure than the input function in python 2. x.

input() function in Python 3.x :

In python 3.x ,we have only one input function that is input() function.

input() function in Python 3.x is not same as input() function in Python 2 .x.

In python 3.x input function work similar to the raw_input()  function. It converts all inputs into a string of STR and returns it. But in python 2. x, its working is different. If we use an input function in python 2. x, it does not convert inputs into the string. It takes value with its data type.

Example: Let’s understand it by taking an example is below:

x = input("Enter the name of the student : ")
print(x)
print(type(x))


# for typecasting 
y = int(input("Enter the marks of the student : "))
print(y)
print(type(y))

Input:

Ram

100

Output:

Enter the name of the student: Ram
Ram
<class 'str'>
Enter the marks of the student: 100
100
<class 'int'>

In this, we are required to perform typecasting explicitly.

Example 2: Let’s understand this typecasting by taking another example:

x=input("Enter first number : ")
y=input("Enter second number : ")
sum = x+y
# concatination of two strings
print(sum)
print(type(sum))


# by using type casting
x=int(input("Enter first number : "))
y=int(input("Enter second number : "))
sum = x+y
# sum of two numbers
print(sum)
print(type(sum))

Input:

1

2

Output:

Enter first number : 1
Enter second number : 2
12
<class 'str'>
Enter first number : 1
Enter second number : 2
3
<class 'int'>

In the above example, it is shown that firstly, we take two numbers by input() function and perform a sum operation, then it will concatenate it like a string because the return type of input() function is a string. After this, we use typecasting, then we take two numbers by input() function and perform a sum operation. Then, it will sum it like an integer because we have used typecasting here.


Related Topics

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

An Introduction to Mocking in Python

Python Mocking is a testing library. It enables you to substitute portions of your system under test with fake(mock) objects and make assertions about how they were utilized.  The test is...

3 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

To Do GUI Application using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

Python Bubble Sort

Bubble sort is one of the techniques used to sort the elements in lists in a certain order, either in ascending or descending order. It is called Bubble sort because...

4 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

Python List reverse() method

 Python List reverse() method The list.reverse () method in Python reverses the elements of the list in place. Syntax list.reverse() Parameter NA Example 1 # Python program explaining # the list.reverse() method weekList = ['Sun','Thurs','Mon','Fri', 'Tues', 'Wed'] print("Actual week list: ",weekList) #...

1 minute read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Curdir Python

Python Programming Language Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python List count() method

Python List count() method The list.count() method returns the number of times x appears in the list. Syntax list.count(x) Parameter x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.) Return This method returns the...

1 minute read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Python vs R

Python: - Python is a popular high-level, general-purpose programming language. Python (the most recent version is Python 3) is a programming language used in the software industry for website development, machine...

3 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Python List append() Method

Python List append() Method The list.append() method in Python adds or appends an item to the end of the list. Syntax list.append(x) Parameter x: It is a required parameter that represents an element of any type (string, number,...

1 minute read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.