Open In App

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

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard.

input() function

Python input() function is used to take the values from the user. This function is called to tell the program to stop and wait for the user to input the values. It is a built-in function. The input() function is used in both the version of Python 2.x and Python 3.x. In Python 3.x, the input function explicitly converts the input you give to type string. But Python 2.x input function takes the value and type of the input you enter as it is without modifying the type. 

Example program in Python3 

Python3




# Python program to demonstrate
# input() function in Python3.x
 
 
val1 = input("Enter the name: ")
 
# print the type of input value
print(type(val1))
print(val1)
 
 
val2 = input("Enter the number: ")
print(type(val2))
 
val2 = int(val2)
print(type(val2))
print(val2)


Input and Output

  

Here, the value “python3” take from the user and store it in the val1 variable. The type of the value stored is always string for input function only for Python 3.x. The value “1997” take from the user and store it in the variable val2. Now, the type of variable val2 is a string and we have to convert the type to an integer using int() function. The val2 variable stores the value “1997” as an integer type. 

Example program in Python2 

Python3




# Python program to demonstrate
# input() function in Python2.x
 
 
val1 = input("Enter the name: ")
print(type(val1))
print(val1)
 
val2 = input("Enter the number: ")
print(type(val2))
print(val2)


Input and Output

  

Here, the value “python3” take from the user and store it in the val1 variable. The function takes the value and type of the input you enter as it is without modifying the type. The type of value in val1 is string type. The value “1997” takes from the user and store it in the variable val2. Now, the type of variable val2 is integer type. We don’t need to explicitly change the variable type.

raw_input() function

Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function. The input function is used only in Python 2.x version. The Python 2.x has two functions to take the value from the user. The first one is input function and another one is raw_input() function. The raw_input() function is similar to input() function in Python 3.x. Developers are recommended to use raw_input function in Python 2.x. Because there is a vulnerability in input function in Python 2.x version

Example program in Python2 

Python3




# Python program to demonstrate
# input() function in Python2.x
 
 
val1 = raw_input("Enter the name: ")
print(type(val1))
print(val1)
 
val2 = raw_input("Enter the number: ")
print(type(val2))
val2 = int(val2)
print(type(val2))
print(val2)


Input and Output

  

Here, the value “python3” take from the user and store it in the val1 variable. The type of the value stored is always string for raw_input function. The value “1997” take from the user and store it in the variable val2. Now, the type of variable val2 is a string and we have to convert the type to an integer using int() function. The val2 variable stores the value “1997” as an integer type.

Let us see the differences in a tabular form -:

  input()  raw_input() 
1. input() function take the user input. raw_input() function takes the input from the user.
2.

Its syntax is -:

input(prompt)

Its syntax is -:

raw_input(input)

3. It takes only one parameter that is prompt. It takes only one parameter that is the input.
4. It return the input that it takes. Its return type is of string.
5. It converts the input into a string by removing the trailing newline It is only introduced in python 2.0 version
6. blocks until input received hang ’till user inputs
7. “hello world” “hello world” but string
8. foo in snake_case


Previous Article
Next Article

Similar Reads

Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Mathematical Functions in Python | Set 4 (Special Functions and Constants)
Some of the mathematical functions are discussed in below set 1, set 2 and set 3 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Special Functions and constants are discussed in this
2 min read
Mathematical Functions in Python | Set 1 (Numeric Functions)
In python a number of mathematical operations can be performed with ease by importing a module named "math" which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number is returned. 2. floor() :- This function returns t
3 min read
Difference Between tf.Session() And tf.InteractiveSession() Functions in Python Tensorflow
In this article, we are going to see the differences between tf.Session() and tf.InteractiveSession(). tf.Session() In TensorFlow, the computations are done using graphs. But when a graph is created, the values and computations are not defined. So a session is used to run the graph. The sessions place the graphs on targeted devices and execute them
3 min read
Difference between input() and sys.stdin.readline()
Python is a widely used general-purpose language that can be used for many purposes. Taking input in any language is as important as breathing for humans. Python provides various methods for taking input. However, we all may get confused about how each method is different from one another. In this article, we will discuss about two such methods i.e
2 min read
How to Measure the Binary Cross Entropy Between the Target and the Input Probabilities in PyTorch?
In this article, we are going to see how to Measure the Binary Cross Entropy between the target and the input probabilities in PyTorch using Python. We can measure this by using the BCELoss() method of torch.nn module. BCELoss() method The BCELoss() method measures the Binary Cross Entropy between the target and the input probabilities by creating
2 min read
Differences between Flatten() and Ravel() Numpy Functions
We have two similar kinds of ways to convert a ndarray to a 1D array of Flatten() and Ravel() Numpy function in Python programming language. Example of Flatten() Numpy function Here, we will create a Numpy array, and then by using flatten function we have changed the element in the flattened 1D NumPy array. C/C++ Code import numpy as np # Create a
3 min read
Autocomplete input suggestion using Python and Flask
In this article, we will learn how to give features like auto-completion to the data that is passed from Flask. Autocomplete basically means predicting the rest of the word when the user types something. This way human interaction also increases with every correct prediction. Let's see how to do the same. We will be using jquery for autocompletion.
2 min read
Replace infinity with large finite numbers and fill NaN for complex input values using NumPy in Python
In this article, we will cover how to fill Nan for complex input values and infinity values with large finite numbers in Python using NumPy. Example: Input: [complex(np.nan,np.inf)] Output: [1000.+1.79769313e+308j] Explanation: Replace Nan with complex values and infinity values with large finite. numpy.nan_to_num method The numpy.nan_to_num method
3 min read