When it comes to real world projects, Taking input from the user is a neccessary task for a program.
Because, It is the user who interacts with the projects and gets desired result associated with their input values.
And, Of course there is no use of such projects where a user does not involve. Right?
In this article, we are going to see how to take input from the user in Python with the help of examples.
So, Let’s get started!
Python provides us two built-in functions to allow to take input from the user.
When I say input from the user, it indirectly indicates taking input values from the keyboard. Because, Any user uses keyboard to enter their input values into the computer.
input() function is used to take input value from the user in python
3.x versions. Python 3 is the latest version of Python.
prompt: It is the string (text or message), which you would like to display on the screen before the input value entered by the user. It is optional.
name = input("What is your name: ") print("My name is:", name)
What is your name: Jeff My name is: Jeff
As you can see in the above example, we are assigning (storing) whatever the user entered into a variable called “name”.
Now, you might be wondering that, why we are assigning the entered value to the “name”. Right?
By doing this, We will be able to access that input value (the value entered by the user) with the help of this variable called “name”.
It’s important that we do this, after all, what’s the point of asking the user for something if we’re not gonna keep track of it.
📝 Note: By default, The return type of the
input() function is a String.
The above statement simply means, When you work with
input() function, it will always give you the string type. Whatever you type (enter) as input either
complex or any other data type,
input() function always convert it into a string.
So, It will always give you the value in string fromat (i.e.,
Let’s check the truthness of this with the help of an example:
num = input("Enter a number: ") type(num) # type() function is used to get the data type of a value or a variable
Enter a number: 512 <class 'str'> # this line indicates that "num" is of string type. which means, "512" is of string type.
To convert the default (i.e.,
str) return type of
input() function to a specific type, you have to use the required functions such as
set(), etc according to your particular situation.
# default string to int conversion num1 = input("Enter a number: ") print(num1) type(num1) num2 = int(input("Enter a number: ")) # using int() function to convert a string into integer type. print(num2) type(num2)
Enter a number: 1122 1122 <class 'str'> # this line indicates that "num1" is of string type. Enter a number: 1122 1122 <class 'int'> # this line indicates that "num2" is of integer type.
Similarly, You can use other functions such as
list() etc to change the default return type of
input() function to your required type according to your particular situation.
How input() function works in python:
input()function executes, program flow will resume to allow the user to type in a line from the keyboard. Once the user presses the Enter key, all the charcater typed are read and returned as string.
Note: The newline generataed when the user presses the Enter key and it is not included as part of the return string.
- If you include the optional
input()displays it on the output screen as prompt to the user before pausing to read input. It is generally used to describe what value is expected from the user or what the user have to do.
input()function always returns a string. If you want a different type, then you need to convert the string to the required type.
raw_input() function is used to take user input in python
2.x versions. Python 2 is the older version of Python.
Python 2 is no longer in development and all new features will added in Python 3.
And, now this
raw_input() function has been deprecated and removed from python 3.
There is no difference between
raw_input() function except their name.
city = raw_input("Enter your city name: ") print(city) type(city)
Enter your city name: Ranchi Ranchi <class 'str'>
In this article, you learned how to take input in Python using
raw_input() functions with the help of examples.
I really hope that you liked my article and got a lot of value from it.
Now, It’s your turn!
If you have any question, feel free to ask in the comment section below right now.
And, and, and…
Don’t forget to share this article to your lovely friends.
Sharing is Caring! 💗