Python raw_input()

Filed Under: Python

Good day, learners. In our previous tutorial we learned about Python SimpleHTTPServer. In this tutorial we will learn about Python raw_input() function.

Python raw_input()

Actually, Python raw_input() function is removed from Python 3.x versions. But it exists in Python 2.x. Actually it has same functionality same as python input() function of Python 3.x.

However, in this tutorial we will try to enlighten on this topic so that you would not surprise if you find raw_input function in python 2.x code. It will also help you if you are migrating your code from python 2 to python 3.

Python 2.x environment setup

To use python raw_input() function, you must have python 2 installed in your system. If you run your program from terminal, then use python2 instead of python or Python3. So the sample command to execute is given below:


$python2 sample.py

It depends on how you have configured your python. Bottom line is that if you are using raw_input function, you have to run your program using python 2.x version.

If you use PyCharm IDE, then you can change your python compiler. To do so, go to File -> Settings -> Project -> Project Interpreter. Then select python 2.x. from the list. Same as the below image.

python raw input environment

Python raw_input example

Here, we will introduce a single python raw_input example program. But you should know at first that the raw_input() function takes string as input. So, we will now write a program that will prompt for your name and then prints it. The code is given below.


a = raw_input('What\'s your name : ')
print 'Username : ', a

Look at the print function closely. The whole code is written for python 2.x version. So, some functions may not seem familiar to you now. However, the output of the code will be like below.


What's your name : Andy Moore
Username :  Andy Moore

python raw_input example

So, that’s all about python raw_input() function. Hope that you get the basic concept about the function.

Reference: Official Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages