Python input 3 numbers in one line

We can get 3 number from user like this

a = int(input())
b = int(input())
c = int(input())

Now how can we get 3 number from user in one line?

a , b , c = int(input())

I tried this but it's not ok

asked Sep 5, 2021 at 12:21

Python input 3 numbers in one line

2

try this:

a , b , c = map(int,(input().split()))

answered Sep 5, 2021 at 12:24

I'mahdiI'mahdi

18.7k5 gold badges19 silver badges28 bronze badges

You can do like this:

a, b, c = input("Insert the 3 values: ").split()
print(f"a: {a}\nb: {b}\nc: {c}")

See this similar question for more details

answered Sep 5, 2021 at 12:24

Python input 3 numbers in one line

If you're reading data from a .txt file and you're wanting to map variables a, b, and c in one line.

Example:

inputFile = open("example.txt",r)

a, b = map(int, inputFile.readline().split())

Python input 3 numbers in one line

bad_coder

9,27119 gold badges37 silver badges61 bronze badges

answered Sep 5, 2021 at 12:29

Python input 3 numbers in one line

try:
    a,b,c  = map(int,input('input').split())

    print(a,b,c)
    
except:
    print('input is no good')

answered Sep 5, 2021 at 12:24

Python input 3 numbers in one line

pippo1980pippo1980

1,5963 gold badges10 silver badges22 bronze badges

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: 
     

    Python input 3 numbers in one line

    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. 

    Python input 3 numbers in one line

    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 : 
     

    Python input 3 numbers in one line

    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 I put 3 inputs on the same line in Python?

    Example -2:.
    # Taking multiple inputs in a single line..
    # and type casting using list() function..
    x = list(map(int, input("Enter multiple values: "). split())).
    print("List of students: ", x).

    How do you input 3 numbers in Python?

    # Python Program to input 3 numbers and display the largest number.
    num1=int(input("Enter First Number")).
    num2=int(input("Enter Second Number")).
    num3=int(input("Enter Third Number")).
    if (num1> num2 and num1> num3):.
    print("The Largest number is", num1).
    elif (num2 > num1 and num2> num3):.

    How do you take multiple integer inputs in one line in Python?

    Syntax :.
    Syntax :.
    input().split(separator, maxsplit) Example :.
    # taking multiple inputs at a time. # and type casting using list() function. x = list(map(int, input("Enter a multiple value: ").split())) ... .
    # taking multiple inputs at a time. x = [int(x) for x in input("Enter multiple value: ").split()].

    How do you take n number inputs in one line in Python?

    You can use a list comprehension to take n inputs in one line in Python. The input string is split into n parts, then the list comp creates a new list by applying int() to each of them.

    Tải thêm tài liệu liên quan đến bài viết Python input 3 numbers in one line