Input() Function in python
9 Dec 2019

How to use input() funtion in Python?

So recently we have seen in Python how to use print () which is generally used to display some output on the monitor or screen. Now let us discuss how to input a value from keyboard – for that input() is used. Syntax is –

  • input([prompt])
    • Where prompt is the string we wish to display on the screen. It is optional.

Let us take one example – say we have created a variable studentname as follows:-

studentname=input(“Enter Student Name”) 

Here studentname is a variable that is instantaneously created in the memory and with the help of a keyboard, you are allowed to input a value over here. This input() will be deeply discussed in Python Training. However, for the first time, we have to understand that whatever has been entered will be treated as a textual input. 

Note: In case you are using lower version of Python 3.2 then you have to use raw_input() 

Now the question comes into our mind that how to enter a number or values which are not of textual type using input() to a variable? Let us discuss it over here –

Suppose you have to enter the day of the month (which is in integer form) and the temperature of the day in decimal form. Let us use two separate variables say Day & Tempr for storing the two values – so we can write the statements as below:-

input () funtion in Python

input () funtion in Python

Day=int(input(“Enter The Day of the Month”))

Tempr=float(input(“Enter the Temperature”))

print(“Day is = “+str(Day))

print(“Temperature = “+ str(Tempr)) 

input () funtion in Python

str() is used to convert a value into string format as in print command everything is to be converted into the string format. Otherwise, you will get error while running this program. Necessary screenshot is given below:-

input () funtion in Python

Here we had to change the type of the values in integer form and in decimal or float type. So we have used type conversion by using int(….) and float(….) before the input(). We will discuss further on this topic in our upcoming python Training session.