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.

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:

#A Demo of telling leap year with input function


    a_Year = float(input("Enter a Year? "))

except ValueError:

    print ("Must enter digits!")


    Leap_Year = a_Year%4 == 0

    if Leap_Year:

        print ("This is a leap year")


        print ("This is not a leap year")


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.

import math


    sqrt_num = float(input("Please Enter a number for calculating the square root? "))

except ValueError:

    print ("Only numbers are allowed - No strings or other characters!")


    print ("The Square root of",sqrt_num ,"=" ,math.sqrt(sqrt_num))


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:

Y_name = input("What is your name? ")

Y_age = input("What is your age? ")

Y_color = input ("What is your favorite color?")

print ("Welcome, %s." % Y_name)

print ("Your age is %s" % Y_age)

print ("Your favorite color is %s" % Y_color)


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.

#The input with eval function example

a_num = eval(input('Give a number to be squared? '))

b_squ = a_num*a_num

print ("The square of the",a_num ,"is" , b_squ)


input eval

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

Author - Atiq Zia

Atiq is the writer at, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!