Python input vs raw_input function

In your Python programs, you may need to take the user input from the users for performing certain actions. For example, asking a user to enter their names, age, Zip code etc.

The Python 3 input function enables us to take the user input from the keyboard. As a line of code containing the input function executes, the user will be prompted to enter the required information.

Leap year example with input  square root example

The input method takes the input (generally what the user types), and returns it back as a string.

The Syntax:


The prompt is an optional parameter. If this is given, it is written to the standard output without a trailing newline.

I will show you examples of using the Python input function with leap years, square roots and other in the later part of this tutorial, first, let me explain the difference between the Python raw_input and input functions.

The difference between input and raw_input

  • The raw_input function was built in Python 2. It is deprecated in version 3.
  • The raw_input worked the same way as input() function in Python 3 works. Alternatively, you can say the raw_input is renamed to input function Python version 3.
  • In Python 2, the input() function was used to first take the raw_input() and then performing an eval() in it i.e. eval(raw_input(prompt)) was equivalent to input(prompt).
  • In Python 3, the purpose of input() in Python 2 is removed and the same result can be achieved by using the eval(input()).

Now, let us see a few examples of using the input Python function.

An example of checking the leap year using Python input function

As you run the following program, the user will be asked to enter a year. As you enter the year and press the enter key, the entered value of the year will be calculated to check whether it is a leap year or not and the result will be displayed on the screen. Have a look:

See online demo and code

Python input

An example of calculating the square root by user input

In this example, the square root of the given number is calculated and displayed after a user enters a valid number.

As returned value by input Python method is a string, so this is first converted to a float and then sqrt() function is used to calculate the square root of the given number. The sqrt() function is the part of math module, so it is also imported.

See online demo and code


Python input sqrt

You can see, the code is placed inside the try-except block, so if a user accidentally enters a wrong value like a string, the program will not crash. Instead, a descriptive message is displayed, notifying the user about the mistake.

A demo of multiple input functions

As you run this simple program, it will ask the name, age and favorite color by the user. Upon entering all information, the program will display the information:

See online demo and code


Python input multiple

An example of using eval function with input

In the following example, the square of a given number is calculated and displayed by using the input function with eval(). I am just showing the very simple use of eval() function while it is a big topic and require its own chapter.

>See online demo and code


Python input eval

You may place an error handler like the above examples to avoid calculating a non-numeric value.