User input is one of the most used functionalities of various computer applications. You will find yourself coding user input many times throughout your career. Thus, today’s tutorial is going to be about Python Input Function.
In this tutorial, I will be covering multiple topics about user input, such as, how to –
- Use Python input function.
- Take integer input from the user in Python
- Accept floating values from the user in Python.
Along with all these, you will also be learning, how to type cast a variable from one data type to another.
We will be using in-built functions of Python libraries to perform all the above-mentioned tasks, so get ready.
To learn how to take user input, first, let’s understand what is this input function?
The Python Input Function.
The input( ) is an in-built function of Python Library which is used for taking input from the user in Python. This function was known by the name of raw_input earlier in Python 2. It was later changed to a much simpler name ‘input’ in Python 3.
The raw_input function is obsolete in Python 3 which means, if you are using the latest version of Python then you will not be able to use it.
The Syntax of Python Input Function
Input has a very simple syntax similar to various other in-built functions of Python library. Take a look –
The Python input function accepts only one parameter and that too is optional. This parameter is prompt. Moreover, the prompt could be any text or expression which prompts the user for input. Although the parameter prompt is optional but using it is highly recommended.
For best practice always try to write an easy to understand prompt, such as – “Enter your e-mail” or “Enter your first name”.
How does the Python input function work?
When Python interpreter comes across an input expression, it first checks whether there is any prompt to display or not. Consequently, if the input function call contains a prompt, then the interpreter prints it on the screen after checking it for any errors.
Once the prompt has been displayed to the user then the interpreter waits for the user to enter the value and press the enter key. After that, the interpreter evaluates the value entered by the user and proceeds ahead to execute the next statement. That is how the Python input function works.
What does Python input function return?
The output of the input function is always a string. The input function of Python library takes the data from the user and then it converts it into a string. After that, you can process that string in whatever way you want.
In case you are not familiar with strings in Python then I suggest you check out these suggested readings –
Where can I find input ( ) function in Python architecture?
The Python input( ) function is defined into the builtins module in Python 3 and __builtin__ in Python 2 just like every other in-built objects, identifiers or methods. You can read more about the builtins module here.
[bctt tweet=”The Python input( ) function is defined into the builtins module in Python 3″ username=”@RebellionRider”]
Hard way to use Python input function.
>>import builtins >>#import __builtin__ >>user-inp = builtins.input(“Enter some value: “)
Examples of Python Input Function!
Now that I have explained all the details of the input function, it’s time to do some practical examples.
How to take a “string input” from the user.
The most common type of input is the string type. Such as username is a string input so is the password. Now suppose you want the user to input his or her name. Therefore code for accepting the name of the user in Python will be –
f_name = input (“Enter your first name:”) print("welcome", f_name)
Here we have two statements. Let’s see what is happening in these two statements—
Statement 1: In the first statement we have the input function call. The string which is written inside the parenthesis (“Enter your first name”) is the prompt which will get displayed on the screen. In the same statement, we have also declared a variable f_name.
On execution, the interpreter will take the input from the user and convert it into the string and then save it into the variable f_name.
Statement 2: the Second statement is a simple print statement which is displaying back the value stored into the variable f_name.
This statement will help us to see if the input function worked as it was desired to be or not?
How to take an “integer input” from the user.
The only short coming of the Python input function is that it converts the inputted value into a string.
Which means if a user enters an integer value then the function will convert it into a string. For example
salary = input (“Please enter your salary: ”) print(type(age))
Ideally the above code should accept an integer value from the user and store it as an integer into the variable. But when you will execute the above code, you will find out that the variable “age” belongs to a string class instead of an integer. In other words the data type of the variable age is string not the integer.
That is because the input function converts the inputted data into string before storing or processing it further.
Python is a dynamically typed programming language. Which means unlike various other high-level programming languages we cannot specify the datatype of the variable while declaring it. In addition the data type of the variable gets decided by the data which is getting stored into it automatically.
Now you asked, so what if it converts the data into string? For the naked human eyes, it doesn’t matter but for Python engine the data type of the variable matters a lot. For example
Let’s say you want to add a 1000$ bonus to the inputted salary. For that you wrote this code
salary = input (“Please enter your salary: ”) bonus = salary + 1000; print(“Your Salary after bonus is: ”)
The reason for that error is, in Python, you cannot perform an arithmetic expression between a string and an integer. According to Python Programming norms, an arithmetic operation can only be performed between two compatible data types like an integer, a float or a real number.
The process of converting the data type of an object from one to another is called typecasting.
Typecasting is the only solution!
Let me tell you that there is no way we can stop Python input function from converting the inputted data to a string. It is going to do that no matter what!
The only solution is to reconvert the inputted data from string to the intended datatype. To do that we can take help of some of the other in-built functions of Python library.
How to do Type casting?
I must say, in Python Programming, type casting an object is very simple. Python provides us with very simple in-built functions to convert the data type of the object. Three most commonly used functions for type casting of Python libraries are –
- int( )
- float ( )
- str ( )
int( ): The int( ) function converts the object of any data type into an integer.
float ( ): As the name suggests, the float ( ) functions convert the object of any data type into a float.
str ( ): The str ( ) function of Python library type casts the object of any data type into a string.
Now that I have explained, what is type casting and how to do that. Let’s come back to our topic and learn how to accept integer input from the user in Python.
There are two ways to type cast the inputted value. You can type cast the value either
- Before it gets stored into the variable or
- After it is stored in the variable.
Let’s learn both these ways.
How to take integer input – Typecast after value gets stored
In this approach, we convert the datatype of the inputted value after it is stored into the variable. As we want the inputted value to be an integer thus we will use the in-built function int ( ) of Python library.
salary = input ("Enter Your Salary: ") bonus = int(salary)+1000 print("After bonus your salary is: ", bonus)
Except for the int function call in statement 2, everything is pretty much the same. In the statement 3, I called the int( ) function and passed the name of the variable “salary” to its parameter.
On execution, the int( ) function call will convert the data type of the value stored into the variable salary into the integer.
How to take integer input – Type cast before value gets stored
This is the most common approach for taking an integer input from the user in Python. In this approach, we typecast the value into an integer before it gets stored into the variable. To do that I will call the int( ) function in the first statement. Like this.
salary = int(input ("Enter Your Salary: ")) bonus = salary+1000 print("After bonus your salary is: ", bonus)
To typecast the value into an integer we need to perform a nesting of function. As you can see we did the same in the above code. We passed the Python input( ) function as the parameter of the int( ) function.
This contraption of nested function call will convert the data type of the value entered by the user from string to an integer before it gets saved into the variable.
That is how we take an integer input using Python input function in Python Programming language.
If you are like me who learns faster by watching videos, then check out this tutorial on YouTube channel.
How to take float input in Python Programming.
In this section I will quickly show you, how you can take a floating value as input using Python input function.
The approach will remain the same, we will simply tweak the type casting. Therefore this time I will type cast the inputted value from string to float instead of integer. To do that we will use the in-built function float( ) of Python library.
salary = float(input ("Enter Your Salary: ")) bonus = salary+1000 print("After bonus your salary is: ", bonus)
On execution of this code, the value which is entered by the user and which is getting stored into the salary variable will be of floating data type.
Info: At any point in any
That is it for this tutorial. We have learnt a lot today. In this tutorial I have demonstrated to you how to:
- Take a string, an integer and a floating input using Python input( )
- Type-cast an object, and
- Use type( ) function and find out the data type of the object
I think that’s enough for this tutorial. If you enjoyed this article, please do share on your social media. Thanks for reading.
Good luck and god speed.