×

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

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

Python Set discard() method

Python Set discard() method The set.discard() method in Python removes a specified element from the set (if present). Syntax set.discard(value) Parameter value- This parameter represents the element to be removed from the set. Return None Example 1 # Python program explaining # the...

1 minute read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

Python OOPs: Class, Object, Inheritance and Constructor

Basic Object-Oriented Programming (OOP) Concepts in Python Python is similar to most general-purpose programming languages with an Object-Oriented environment system since its existence. Being an Object-Oriented Programming language, Python provides ease...

9 minutes read.

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

Global variables in python

In Python, considering the scope, variables are categorized into global and local variables. In this article, we will discuss these two types along with examples. Any variable holds a value in...

4 minutes read.

Weight Conversion GUI 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.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

Python eval() function

Python eval() function The eval() function in Python is used to evaluate the given expression. If the specified expression is a legal Python statement, it will be executed. Syntax eval(expression, globals=None, locals=None) Parameter expression: This parameter represents a String,...

1 minute read.

How to check if the dictionary is empty in Python?

What is a dictionary in Python? A directory is a collection of data but data is not ordered. Unlike other data types, it does not hold a single value as its...

3 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

Python's Qstandarditemmodel

Python More interactive and user-friendly than any other programming language is Python. Many libraries are used by the python programming language to speed up procedures. Python can be used to develop...

3 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Python Sys Stdout

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Bitwise Operators

When used with variables and objects in an expression, operators are tokens that carry out calculations. The variables and items to which the computation is applied are known as operands....

5 minutes read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 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.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.