How to take integer input in Python?

In this post, We will see how to take integer input in Python. As we know that Python 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’s see the examples:

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# take input from user
input_a = input()
  
# print data type
print(type(input_a))
  
# type cast into integer
input_a = int(input_a)
  
# print data type
print(type(input_a))

chevron_right


Output:



100
<class 'str'>
<class 'int'>

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# string input
input_a = input()
  
# print type
print(type(input_a))
  
# integer input
input_b = int(input())
  
# print type
print(type(input_b))

chevron_right


Output:

10
<class 'str'>
20
<class 'int'>

Example 3:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# take multiple inputs in array
input_str_array = input().split()
  
print("array:", input_str_array)
  
# take multiple inputs in array
input_int_array = [ int(x) for x in input().split()]
  
print("array:", input_int_array)

chevron_right


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]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.