How To Prompt User For Input In Python?

Python is a programming language with many features and capabilities. One feature that makes it unique is the ability to prompt a user for input. In this blog post, we’ll show you how to do that in Python.

Checkout this video:


Asking the user for input is a common task in Python programming. Sometimes you will need to get input from the user, for example, to ask them for a file name or a directory path. To do this, you can use the input() function.

The input() function prompts the user for input and returns it as a string. For example:

>>> input(“Enter your name: “)
Enter your name: John
If you want to prompt the user for numeric input, you can use the int() function to convert the string to an integer. For example:

>>> num = int(input(“Enter a number: “))
Enter a number: 42

Python input() Function

The input() function in Python 3 allows you to accept user input. The result is stored in a variable. You can use the type() function to verify the data type of the variable.

In Python 2, you have a function called raw_input(). This function generates the same result as the input() function in Python 3.

Python raw_input() Function

Python raw_input() function reads a line from input (usually user), converts it into a string and returns it. If EOF (end-of-file object) is read then None is returned.

We can take input from the user in many ways in Python. One way is using the input() function. But, sometimes, we may need to have more control over how we obtain data from the user. In such cases, we can use the raw_input() function.

The syntax of raw_input() is as follows:
Here, prompt is optional. It contains a string that is printed on the screen. It serves as a guide for the user and also tells him/her what is expected as an input.
For example:
name = raw_input(“What’s your name? “)
age = int(raw_input(“How old are you? “))
print(“Hello, ” + name + “!”)
print(“You’ll be ” + str(age+1) + ” next year.”)

Difference between input() and raw_input()

Python provides twoBuilt-in functions to read input from keyboard.

input() raw_input()
1. If we want to take input from the user in Python 2.x, we have to use raw_input(). If we want to take input from the user in Python 3.x, we have to use input().
2. raw_input() returns a string, always. input() evalutes the input as a python expression and if the input is not a valid python expression, it raises SyntaxError.
3. Syntax of raw_input(): raw_input([prompt]) where prompt is optional. Syntax of input(): input([prompt]) where prompt is optional.
4. We can pass anything as prompt iher as a string or variable whose value is string but passing an empty string is not recommended as it will make our program look weird because no prompt will be displayed on the screen and our program will just hang there until we give some sort of input from our side via keyboard or mouse click or whatever interface we are using for running our python script which takes some kind of input from the userSide whether it’s graphical(GUI) or Command line based(CLI).

Getting User Input in Python

There are multiple ways to get user input in Python, which we will discuss in this article.

One common way to get input from the user is by using the input() function. This function will prompt the user forinput, and will store that input as a string data type. For example:

user_input = input(“Please enter your name: “)
print(“Your name is”, user_input)
You can also define what data type you want the input to be stored as. For example, if you want the user to enter an integer, you can use the int() function:

user_input = int(input(“Please enter a number: “))
print(“The number you entered is”, user_input)
If you want the user to enter a floating point number, you can use the float() function:

user_input = float(input(“Please enter a decimal number: “))
print(“The decimal number you entered is”, user_input)

Handling Errors with Python User Input

There are many ways to handle errors when getting user input in Python. The most common way is to use the try-except statement. This will allow your program to continue running even if the user enters invalid input.

The general format for the try-except statement is:

# code that might cause an error
# code that will run if there is an error


In conclusion, to prompt a user for input in Python, you can use the input() function. This function will display a message to the user, and wait for them to enter some text. Once the user has entered some text, it will be stored in a variable for you to use in your program.


To get input from the user in Python, you can use the input() function. This function takes a string as an argument and displays it to the user. The string you pass to the input() function is displayed as a prompt, and the user can enter their response on the keyboard. The input() function returns the string that the user enters.

Scroll to Top