How do you input integers in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • 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]


    • Theory
    • Steps
    • Problems

    1. How to read and write in Python

    Every program is eventually a data processor, so we should know how to input and output data within it. There exists a function, print(), to output data from any Python program. To use it, pass a comma separated list of arguments that you want to print to the print() function. Let's see an example. Press "run" and then "next" to see how the program is being executed line by line:

    print(5 + 10)
    print(3 * 7, (17 - 2) * 8)
    print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
    print(37 / 3)  # single forward slash is a division
    print(37 // 3)  # double forward slash is an integer division
            # it returns only the quotient of the division (i.e. no remainder)
    print(37 % 3)  # percent sign is a modulus operator
            # it gives the remainder of the left value divided by the right value
    

    To input data into a program, we use input(). This function reads a single line of text, as a String.

    Here's a program that reads the user's name and greets them:

    print('What is your name?')
    name = input()  # read a single line and store it in the variable "name"
    print('Hi ' + name + '!')
        

    Advertising by Google, may be based on your interests

    2. Sum of numbers and strings

    Let's try to write a program that inputs two numbers and prints their sum. We read the two numbers and store them in the variables a and b using the assignment operator =. On the left side of an assignment operator we put the name of the variable. The name could be a string of latin characters (A-Z, a-z, 0-9, _) but must start with a letter in the range A-Z or a-z. On the right side of an assignment operator we put any expression that Python can evaluate. The name starts pointing to the result of the evaluation. Read this example, run it and look at the output:

    a = input()
    b = input()
    s = a + b
    print(s)
        

    After running the example we can see that it prints 57. As we were taught in school, 5 + 7 gives 12. So, the program is wrong, and it's important to understand why. The thing is, in the third line s = a + b Python has "summed" two strings, rather than two numbers. The sum of two strings in Python works as follows: they are just glued one after another. It's also sometimes called "string concatenation".

    Do you see in the variable inspector, on the right hand side, that the values bound to variables a and b are wrapped in quotes? That means that the values there are string, not numbers. Strings and numbers are represented in Python differently.

    All the values in Python are called "objects". Every object has a certain type. The number 2 corresponds to an object "number 2" of type "int" (i.e., an integer number). The string 'hello' corresponds to an object "string 'hello'" of type "str". Every floating-point number is represented as an object of type "float". The type of an object specifies what kind of operations may be applied to it. For instance, if the two variables "first" and "second" are pointing to the objects of type int, Python can multiply them. However, if they are pointing to the objects of type str, Python can't do that:

    first = 5
    second = 7
    print(first * second)
    
    # you can use single or double quotes to define a string
    first = '5'
    second = "7"
    print(first * second)
        

    To cast (convert) the string of digits into an integer number, we can use the function int(). For example, int('23') gives an int object with value 23.

    Given the information above, we can now fix the incorrect output and output the sum of the two numbers correctly:

    a = int(input())
    b = int(input())
    s = a + b
    print(s)
        

    Advertising by Google, may be based on your interests

    How do you input integer data 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.

    How do you write an integer in Python?

    Python int().
    int() Syntax. The syntax of the int() method is: int(value, base [optional]).
    int() Parameters. int() method takes two parameters:.
    int() Return Value. The int() method returns:.
    Example 1: Python int() with a Single Argument. ... .
    Example 2: int() with Two Arguments. ... .
    Example 3: int() for custom objects..

    How do you input an integer as a user?

    To use them as integers you will need to convert the user input into an integer using the int() function. e.g. age=int(input("What is your age?")) This line of code would work fine as long as the user enters an integer.

    How do you input a long int in Python?

    Type int(x) to convert x to a plain integer. Type long(x) to convert x to a long integer.