Python Raw Input

Python is a programming language used in creating software prototypes, data science, web development service, and many others. It is said to be one of the easiest and best paid programming language that anyone can learn. Learning Python is also learning how to use the raw_input function.

What is raw input?

Python raw input is a function that is used to read data from a standard input like the keyboard.  The raw input function mainly works by presenting a prompt to the user, gets answers from the user, and returns the answer by the user.

In Python 3, raw input has been removed and simply replaced, but this still exists in Python 2. In Python 3, this function is now simply called input. So, if you are using the raw input function, you must have Python 2 installed on your computer.

Here’s an example of using the raw_input function:

If you want to ask a user to input their name and then print it, type the command below:

  • a = raw_input(What\’s your name : ‘)

print ‘Username : ‘, a

Now, the output of this command will be like the one below:

  • What’s your name: User’s name

Username: User’s name

Comparing raw_input and input functions

As what is mentioned above, the difference of these two is the version of Python being used. For Python 2, the raw_input function is used to get string input from a user through the command line while the input function is to evaluate the input string.

For the newer version, Python 3, the raw_input function is removed and replaced by the input function, but the function remains the same, to obtain the users through the keyboard. However, the input function in Python 2 has been discontinued, and to obtain the same functionality, users must use or add the “eval (input()) instead in Python 3.

Example of raw_input function in Python 2

  • txt = raw_input(“Type something just to see if this works: “)

print “Is this what you just said?”, txt

Your output should be like this:

  • Type something just to see if this works: test

Is this what you just said? test

Another simplified sample of raw_input function in Python 2:

  • mydata = raw_input(Prompt :’)

print (mydata)

Example of raw_input function in Python 3

  • txt = input(“Type something just to see if this works: “)

print(“Is this what you just said?”, txt

Your output should look like this:

  • Type something just to see if this works: test

Is this what you just said? test

Another simplified sample of raw_input function in Python 3:

  • mydata = input(Prompt :’)

print (mydata)

When you are using Python 2, just remember to use the raw_input function to get the values from the users while being able to use the input function when using the Python 3 version.

With the use of the above code examples, obtaining information from the user is made easy. But, if you are just learning Python programming language, it is best to make use of all available resources from forums and Python communities online.