Open In App

How to take integer input in Python?

Last Updated : 08 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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))


Output:

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

Example 2:

Python3




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


Output:

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

Example 3:

Python3




# 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)


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




# Python program to take integer input  in Python
 
# input size of the list
n = int(input("Enter the size of list : "))
# store integers in a list using map, split and strip functions
lst = list(map(int, input(
    "Enter the integer elements of list(Space-Separated): ").strip().split()))[:n]
print('The list is:', lst)   # printing the list


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]


Previous Article
Next Article

Similar Reads

Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then the order of matrix is given by r x c. Each entrie
3 min read
Take input from stdin in Python
In this article, we will read How to take input from stdin in Python. There are a number of ways in which we can take input from stdin in Python. sys.stdininput()fileinput.input()Read Input From stdin in Python using sys.stdin First we need to import sys module. sys.stdin can be used to get input from the command line directly. It used is for stand
2 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Step 1: First, we will take the data from the user and
2 min read
How to Take Only a Single Character as an Input in Python
Through this article, you will learn how to accept only one character as input from the user in Python. Prompting the user again and again for a single character To accept only a single character from the user input: Run a while loop to iterate until and unless the user inputs a single character.If the user inputs a single character, then break out
3 min read
Python | Pandas TimedeltaIndex.take()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.take() function return a new Index of the values selected by the indices. We generally pass a list of indices to b
2 min read
Python | Pandas Series.take()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.take() function return the elements in the given positional indices along an ax
3 min read
Python | Numpy matrix.take()
With the help of Numpy matrix.take() method, we can select the elements from a given matrix by passing the parameter as index value of that element. It will return a matrix having one dimension. Remember it will work for one axis at a time. Syntax : matrix.take(index, axis) Return : Return matrix of selected indexes Example #1 : In this example we
1 min read
How to take screenshot using Selenium in Python ?
Selenium offers a lot of features and one of the important and useful feature is of taking a screenshot. In order to take a screenshot of webpage save_screenshot() method is used. save_screenshot method allows user to save the webpage as a png file. Syntax : driver.save_screenshot("image.png") Argument : filename or the full path you wish to save y
1 min read
Create a Python Script Notifying to take a break
We generally do not take breaks when we are using our laptop or PC. It might affect our eyesight as well as mind. So with Python, we can make a program that can notify us that we have to take a break start again after sometime when the user again starts working on the laptop. Modules neededpyttsx3 - It is a text-to-speech conversion library in Pyth
4 min read
Take and convert Screenshot to PDF using Python
In order to take and convert a screenshot to PDF, firstly the PyAutoGUI can be used which is an automation library in python which can control mouse, keyboard and can handle many GUI control tasks. Secondly, for the conversion PIL(Python Imaging Library) of python can be used which provides image processing facility and it supports many file format
3 min read
Practice Tags :