This is a detailed tutorial on taking input in python from the users in the command line interface. Input methods for Python Version 2 & 3 are illustrated.
Taking Input In Python
In our programs, we often need to take the input from the user. Different programming languages provide different options to get inputs from the user. Python provides a very simple method to get input from the user. The user can type their input in the command line and you can receive it in your program.
- The older versions of Python (2.x), makes use of the
raw_input()function to take the input from the user.
- The newer versions of Python (3.x), uses the
input()function for the same.
Both of these functions will return the input that the user types in the command line window.
This function can only be used in Python Versions 3.x. The usage of this function is very easy. You just have to type this function wherever in your program code, you want to take the input from the user. You can use a python variable to store the user input. The following example illustrates the use of the
print("Enter a Input: ")
userInput = input()
In the above code, we’ve stored the user input the variable named
userInput and then used the same variable to print it out on the command line. The
input() function can also take a parameter of String type which is basically the string to display to the user asking the user to enter input for that particular command. This is illustrated in the example given below.
anything = input("Enter Anything Here: ")
print("You Entered: " + anything)
This function only works for Python Versions 2.x. The usage of raw_input() is the same as the usage of input() function, just the difference is in the name of the function itself. The following example illustrates the usage of this function.
print("Enter Anything Here:")
userInput = raw_input()
You can also provide the input asking statement as a string parameter in the
raw_input("String to ask the user the input") function as well, exactly similar to the
Input Data Type
Both of these functions return the user input as a string. Even if you’re taking inputs like integer, you must explicitly convert them into the specific data types using corresponding functions. Let’s understand this concept with the help of an example.
print("Enter an Integer:")
integer = input()
print("Enter a String:")
string = input()
print("Type of the variable integer: " + str(type(integer)))
print("Type of the variable string: " + str(type(string)))
You can clearly see in the output of the above program that even the user enters an integer or a string, the input returned by the input() function is of data type String. Similar is the case with raw_input() function as well. So, you must convert the user input into the required data type explicitly yourself using type conversions.
I’ve modified the above code in order to accept the integer as the
int data type. I’ve used the int() function and put the input() function inside it to store the user input as an integer into the variable
Now, you can see the variable
integer holds the data in data type
int. This way you can explicitly convert the string type user input into any data type, just make sure the user enters the right data type literals otherwise, the program will raise errors. Therefore, it is always good to use Try Except while taking user input to handle the exceptions.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!