Open In App
Related Articles

Taking input in Python

Improve Article
Save Article
Like Article

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 ( prompt )
  • raw_input ( prompt )

input (): This function first takes the input from the user and converts it into a string. The type of the returned object always will be <class ‘str’>. It does not evaluate the expression it just returns the complete statement as String. For example, Python provides a built-in function called input which takes the input from the user. When the input function is called it stops the program and waits for the user’s input. When the user presses enter, the program resumes and returns what the user typed. 


inp = input('STATEMENT')
1.  >>> name = input('What is your name?\n')     # \n ---> newline  ---> It causes a line break
            >>> What is your name?
            >>> print(name)
            # ---> comment in python


# Python program showing
# a use of input()
val = input("Enter your value: ")



Taking String as an input:


name = input('What is your name?\n')     # \n ---> newline  ---> It causes a line break


What is your name?

How the input function works in Python : 

  • When input() function executes program flow will be stopped until the user has given input.
  • The text or message displayed on the output screen to ask a user to enter an input value is optional i.e. the prompt, which will be printed on the screen is optional.
  • Whatever you enter as input, the input function converts it into a string. if you enter an integer value still input() function converts it into a string. You need to explicitly convert it into an integer in your code using typecasting



# Program to check input
# type in Python
num = input ("Enter number :")
name1 = input("Enter name : ")
# Printing type of input value
print ("type of number", type(num))
print ("type of name", type(name1))


raw_input(): This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, converts it to string, and then returns it to the variable in which we want to store it.



# Python program showing
# a use of raw_input()
g = raw_input("Enter your name : ")
print g


Here, g is a variable that will get the string value, typed by the user during the execution of the program. Typing of data for the raw_input() function is terminated by enter key. We can use raw_input() to enter numeric data also. In that case, we use typecasting. For more details on typecasting refer this

Note: input() function takes all the input as a string only

There are various function that are used to take as desired input few of them are : –

  • int(input())
  • float(input())


num = int(input("Enter a number: "))
print(num, " ", type(num))
floatNum = float(input("Enter a decimal number: "))
print(floatNum, " ", type(floatNum))




Refer to the article Taking list as input from the user for more information. 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 May, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials