The raw_input function is used to prompt users for text input and store that input as a string variable in your Python programs. In this tutorial, I will explain how to use the raw_input function in Python to get user input from the command line.
What is the raw_input Function in Python?
The raw_input function is a built-in Python function that prompts the user to type in some text at the command line and returns that text as a string. It allows your program to pause and wait for the user to enter input before continuing execution.
Here’s the basic syntax of the raw_input function:
user_input = raw_input("Enter some text: ")When this line of code is executed, it will display the prompt message “Enter some text: ” in the console and wait for the user to type something and press Enter. Whatever text the user types will be returned as a string and stored in the user_input variable.
One key thing to note is that raw_input explicitly converts the entered data into a string, regardless of what the user types in. So even if the user enters a number like “42”, it will be stored as the string “42” rather than the integer value 42.
Check out How to Iterate Through Tuples in Python?
raw_input in Python Example
To understand how raw_input can be used to solve real programming problems, let’s walk through an example. Imagine you are building a simple command-line application to help a small business in the USA manage their customer data. One feature you want to add is the ability to input a new customer’s name and email address.
Here’s how you could use raw_input to implement this:
print("Enter a new customer (type 'quit' to exit)")
while True:
name = raw_input("Customer name: ")
if name == 'quit':
break
email = raw_input("Customer email: ")
print("New customer added:")
print("Name: " + name)
print("Email: " + email)
print("")In this code:
- We start with a
printstatement to explain to the user how to exit the input loop by typing ‘quit’. - We enter a
whileloop that will keep prompting for new customers until the user types ‘quit’ for the name. - Inside the loop, we use
raw_inputto prompt for the customer name and email, storing them as variables. - After getting a new name and email, we print out a confirmation message displaying the entered data.
- The loop continues, prompting for additional customers until ‘quit’ is entered for the name.
Here’s an example of what the user would see when running this code:
Enter a new customer (type 'quit' to exit)
Customer name: John Smith
Customer email: [email protected]
New customer added:
Name: John Smith
Email: [email protected]
Customer name: quitAs you can see, the raw_input function made it easy to prompt the user for each piece of data we needed and store their responses as strings for later use.
Read How to Calculate the Dot Product in Python Without NumPy?
Validate User Input with raw_input in Python
One potential issue with reading user input is validating that the entered text meets your program’s expectations. Since raw_input always returns a string, you may need to do some additional parsing and error checking depending on what type of data you are expecting.
For example, let’s say we wanted to extend our customer input example to also prompt for the customer’s age. We would expect the user to enter a valid integer value. Here’s one way we could handle that:
while True:
age_str = raw_input("Customer age: ")
try:
age = int(age_str)
except ValueError:
print("Invalid age, please enter a number.")
continue
if age < 0:
print("Invalid age, please enter a positive number.")
continue
else:
break
print("Customer age:", age)The key steps here are:
- We prompt the user for their age using
raw_input, storing the result in a string variableage_str. - We use a
try/exceptblock to attempt convertingage_strto an integer usingint(). If this fails due toage_strnot being a valid number, aValueErroris raised. - If a
ValueErroroccurs, we catch it, print an error message, and usecontinueto go back to the start of the loop and prompt again. - If the integer conversion succeeds, we next check if the
agevalue is negative. If so, we again print an error and continue the loop. - If we make it past the error checks, we know
ageis a valid non-negative integer, so we break out of the loop and continue with our program.
By adding validation logic around our raw_input prompts, we can ensure that our program only proceeds if the user provides the expected type of input. This makes our code more robust and user-friendly.
Conclusion
The raw_input function is used to get text input from users in Python programs. Whether you are building a command-line utility, a text-based game, or any other type of interactive Python application, raw_input provides a convenient way to prompt users for data and incorporate their responses into your program’s flow. In this tutorial, I explained how to use the raw_input function in Python for user input.
You may also like:
- Fastest Sorting Algorithm in Python
- How to Get Values from a JSON Array in Python?
- How to Send Emails Using Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.