How to take two integer input in same line in python

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

Python Basic: Exercise-134 with Solution

Write a Python program to input two integers in a single line.

Sample Solution-1:

Python Code:

print("Input the value of x & y")
x, y = map(int, input().split())
print("The value of x & y are: ",x,y)

Sample Output:

Input the value of x & y
 2 4
The value of x & y are:  2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

Sample Output:

Input the value of a & b:  2 4
The value of a & b are: 2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to calculate the time runs (difference between start and current time)of a program.
Next: Write a Python program to print a variable without spaces between values.

Python: Tips of the Day

You can turn a set into an immutable set

This way, you can no longer modify it:

my_set = frozenset(['a', 'b', 'c', 'd'])

my_set.add("a")

If you do that, an error will be thrown:

AttributeError: 'frozenset' object has no attribute 'add'

Ref: https://bit.ly/3ndmjEN


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    For instance, in C we can do something like this:

    One solution is to use raw_input() two times.

    Another solution is to use split()

    Note that we don’t have to explicitly specify split(‘ ‘) because split() uses any whitespace characters as a delimiter as default.

    One thing to note in the above Python code is, both x and y would be of string. We can convert them to int using another line

    x, y = [int(x), int(y)]
    
    # We can also use  list comprehension
    x, y = [int(x) for x in [x, y]]
    

    Below is complete one line code to read two integer variables from standard input using split and list comprehension

    x, y = [int(x) for x in input().split()]  

    x, y = map(int, input().split())

    This article is contributed by Abhishek Shukla. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods. 

    • Using split() method
    • Using List comprehension

    Using split() method : 
    This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, users use a split() method to split a Python string but one can use it in taking multiple inputs.

    Syntax : 

    input().split(separator, maxsplit)

    Example : 

    Python3

    x, y = input("Enter two values: ").split()

    print("Number of boys: ", x)

    print("Number of girls: ", y)

    print()

    x, y, z = input("Enter three values: ").split()

    print("Total number of students: ", x)

    print("Number of boys is : ", y)

    print("Number of girls is : ", z)

    print()

    a, b = input("Enter two values: ").split()

    print("First number is {} and second number is {}".format(a, b))

    print()

    x = list(map(int, input("Enter multiple values: ").split()))

    print("List of students: ", x)

    Output: 
     

    How to take two integer input in same line in python

    Using List comprehension : 
    List comprehension is an elegant way to define and create list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user. 

    How to take two integer input in same line in python

    Example: 

    Python3

    x, y = [int(x) for x in input("Enter two values: ").split()]

    print("First Number is: ", x)

    print("Second Number is: ", y)

    print()

    x, y, z = [int(x) for x in input("Enter three values: ").split()]

    print("First Number is: ", x)

    print("Second Number is: ", y)

    print("Third Number is: ", z)

    print()

    x, y = [int(x) for x in input("Enter two values: ").split()]

    print("First number is {} and second number is {}".format(x, y))

    print()

    x = [int(x) for x in input("Enter multiple values: ").split()]

    print("Number of list is: ", x) 

    Output : 
     

    How to take two integer input in same line in python

    Note: The above examples take input separated by spaces. In case we wish to take input separated by comma (, ), we can use the following: 

    Python3

    x = [int(x) for x in input("Enter multiple value: ").split(",")]

    print("Number of list is: ", x) 

    Please see https://ide.geeksforgeeks.org/BHf0Cxr4mx for a sample run.
     


    How do you enter two integers on the same line in Python?

    One solution is to use raw_input() two times. Note that we don't have to explicitly specify split(' ') because split() uses any whitespace characters as a delimiter as default.

    How do you get two inputs in one line Python?

    a) split () split( ) function helps us get multiple inputs from the user and assign them to the respective variables in one line. This function is generally used to separate a given string into several substrings.

    Can you have two inputs in Python?

    With the help of the split () function, developers can easily collect multiple inputs in Python from the user and assign all the inputs to the respective variables.