
Getting input from the user in the programs we write can lead to more usability and improved software performance. These days, in most programs, we need to receive various information from the user. In almost all programming languages, various functions and facilities are provided for taking input from the user. Getting input in Python is a lot easier than you think. In this post, I'll review the types of use of input in Python.
Interaction with the user is becoming more important day by day. Many of the Python programs we develop require input. Since these programs may not have a user interface, it is better to receive user inputs in the program execution environment (console).
In the Python programming language, a simple function is considered to take input from the user. This function is built-in
. That is, you won't need to import any library or additional code to the program to use it.
Input statement in Python
In Python language, we can receive the desired input from the user with the help of the input()
function.
When Python code execution reaches the input()
function, the Python interpreter waits for the user to enter the desired input. After that, the program will continue to run.
input()
After this function is called, the user can enter the desired characters in the console and press the Enter key
to announce the completion of entering the information.
In Python version 2, there was a raw_input()
function to receive input from the user. In version 3, this function was removed and input()
replaced it. The features and functionality of these two functions are exactly the same.
The output of the input
function
The output of this function is a String
. This string is exactly the input that the user entered in the program.
The output type of the function cannot be changed. In the following, I'll review the methods to convert the input data type of the input
function in Python.
Getting data from the user in Python
We may want to display a message to the user before receiving input from the user.
The input of the input
function is a string. Python prints the input string to the console and then waits for input before waiting for input.
The input argument of this function is optional. That is, if we don't give any message to the function to print, the function will be executed without any problem.
String input in Python
As mentioned, the output of the input()
function is always a string.
Suppose I want to ask the user's name at the beginning of a program and greet the user. The following code does the same.
name = input("Please Enter Your name: ")
print("Hello " + name + "!")
After running this program, a message to enter the name will be displayed first. After entering the name, a greeting message will be printed.
The following text is an example of the implementation of this program.
Please Enter Your name: Albro
Hello Albro!
Getting a number from the user in Python
The input()
function always gives us a string to get input in Python. For example, if we want to take a number from the user and raise it to a power, we should have a code similar to the code below.
num = input("Enter Number: ")
print(num**2)
But by running this program, I'll see that after entering the number, we will encounter an error similar to the error below!

What does this error mean?
This error means that the exponentiation operator is not valid for number and string! Because the number I got from the input is saved as a string, I get this error!
But what is the solution?
To solve this problem, I can convert the input value to my desired data type.
For example, if I want to have the value of the num
variable as an integer, I should use the int()
function in Python.
This function takes an input value (a string containing only numbers or some other numeric type) and returns an integer.
For example, if we have the string '123'
, we will get the number 123
by calling int('123')
.
int('123')
So my program to get the input number in Python will be something like the following code.
num = input("Enter Number: ")
print( int(num)**2 )
Getting a decimal number from the user
If I want to receive a decimal number from the user, the int()
function will not respond! Because this function gives me an integer.
Similar to this function, there is a function called float()
in Python. With the help of this function, values can be converted into decimal numbers.
Taking multiple entries in one line
Sometimes we need to receive several different values as input from the user in one line and once.
For example, suppose I want to get the user's first and last name separately.
Getting multiple inputs with Separator
The first method is to use the split()
function. This function applies to a string. According to the parameter it took as input, it separates the string.
If no argument is given as input to the function, the space character (Space
) will be considered as separator.
name, family = input("Enter Your name: ").split()
After execution, I give the following value as input to the program.
Hive Blockchain
And at the end, I print the values stored in the variables. The printed values are specified as comments in the following code.
print(name)
print(family)
# Hive
# Blockchain
Getting multiple numbers at the input
With the help of the split()
function, you can specify any type of separator for the input value.
If I want to distinguish and receive three different numbers with the help of the ,
sign; You can use the following code.
x, y, z = input( "Enter Numbers: " ).split(',')
The sample input and values stored in each Python variable will be as follows.
Enter Numbers: 21,25,23
# x = '21'
# y = '25'
# z = '23'
Of course, note that the stored values are in the form of String, and you can use the int()
function to convert them to a number.
Getting list from user in Python
Sometimes I need to receive an unspecified number of inputs from the user. One of the best ideas for this is to use lists.
There are two ways to get a list of data. In the following, I'll explain how to implement each one.
The first method: using the loop to get the list from the user
With the help of a loop in Python and the input()
function, we can receive an unlimited number of inputs and add them to our list.
In this case, we must specify a character as the end character. If the user enters the desired character, the loop will be exited and the program will continue.
data_list = []
while True:
data = input()
if data == "/":
break
data_list += [data]
print(data_list)
In this example, it ends by entering the sign /
getting input.
The second method: using the Python list builder
In this method, with the help of the method of receiving multiple entries in one line and the list in Python, we receive an unknown number of data in one line.
Suppose I want to get some integers and store them in data_list
.
The following code will do this for us.
data_list = [ int(x) for x in input("Enter multiple value: ").split() ]
Summary: Getting input in Python
Input from the user can be obtained using the input()
function in Python. This function takes an optional input, which it will print before waiting for input. Anything entered as input data is returned to us by this function as a text string in Python. This value can be stored in a variable or converted to desired data types with the help of functions such as int()
.
Using the split()
function, it is also possible to receive multiple input values from the user with a specified separator and store each one in separate variables.