View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this post, We will see how to take integer input in Python. As we know that Python’s built-in input[] function always returns a str[string] class object. So for taking integer input we have to type cast those inputs into integers by using Python built-in int[] function.
Let us see the examples:
Example 1:
Python3
input_a
=
input
[]
print
[
type
[input_a]]
input_a
=
int
[input_a]
print
[
type
[input_a]]
Output:
100
Example 2:
Python3
input_a
=
input
[]
print
[
type
[input_a]]
input_b
=
int
[
input
[]]
print
[
type
[input_b]]
Output:
10 20
Example 3:
Python3
input_str_array
=
input
[].split[]
print
[
"array:"
, input_str_array]
input_int_array
=
[
int
[x]
for
x
in
input
[].split[]]
print
[
"array:"
, input_int_array]
Output:
10 20 30 40 50 60 70 array: ['10', '20', '30', '40', '50', '60', '70'] 10 20 30 40 50 60 70 array: [10, 20, 30, 40, 50, 60, 70]
Example 4:
Python3
n
=
int
[
input
[
"Enter the size of list : "
]]
lst
=
list
[
map
[
int
,
input
[
"Enter the integer elements of list[Space-Separated]: "
].strip[].split[]]][:n]
print
[
'The list is:'
, lst]
Output:
Enter the size of list : 4 Enter the integer elements of list[Space-Separated]: 6 3 9 10 The list is: [6, 3, 9, 10]
Now, you know how to print something on the screen and not just that, you also know about the different data types available in Python and how to use them. Let’s move forward with this chapter and learn a new Python concept.
Till now, we have assigned values to variables in the code. However, there can be times when we want the user to enter the value which has to be assigned to a variable. Take an example of a calculator in which the user enters the values to be added or subtracted.
input[]
function is used to take input from the user. So, let’s see how to take values entered by the user.
The statement in the above code will read the value entered by the user [xyz] and will assign it to the variable name
.
We can also print a message on the screen while asking the user for input by passing that message to the input[]
function.
name = input["What is your name >>>"]
Output
In this example, the message
“What is your name >>>” will get printed on the screen and the value entered by the user after that will be assigned to the variable name
. The message displayed on the screen while taking input is helpful to understand what type of input the user is expected to enter. Therefore, it is a good practice to display such messages while taking input.
Let’s look at some other examples.
print["Enter your name"] x = input[] y = input["age >>>"] #age>>> will be printed before input print["Your name is",x,"and","and your age is",y]
Output
Enter your name
xyz
age >>>20
Your name is xyz and and your age is 20
print["Enter your wish"] wish = input[] print["May your wish come true!"]
Output
Enter your wish
I want to be greatest coder ever
May your wish come true!
What does input[] do?
x = input[">>>"] print[x]
Output
If the user enters 10, then the first statement is equivalent to x = '10'
.
We gave 10 to input[]
. You can think that input[]
becomes 10. So, x = input[]
will be
equivalent to x = 10
after the input is given.
Taking String Input in Python
By default, any input entered by the user is of type string.
num = input[] print[type[num]]
Output
As you can see, the user entered a number 10 as input but its type is str. Therefore, any input entered by the user is always read as string.
Taking Integer Input in Python
We just saw that input is always read as a string. So what if we want to read an integer?
We can do this by converting the string input to int using the int[]
function.
x = int[input["Enter an integer >>>"]] print ["You have entered", x]
Output
Enter an integer