Input on same line 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.

Simple example code

n = 2  # how many numbers to accept
numbers = [int(num) for num in input().split(" ", n-1)]

print(numbers)

Output:

Input on same line python

The following snippet will map the single line input separated by white space into a list of integers

lst = list(map(int, input().split()))

print(lst)

Output:

1 2 3
[1, 2, 3]

How to take multiple inputs of different data types in one line in Python?

Answer: Example take 2 input values.

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

print(x, y)

Output:

Enter a two value: 1 X
1 X

OR

score, name = int(input('Enter Score: ')), input('Enter name:')

print(score)
print(name)

Do comment if you have any doubts and suggestions on this Python input topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Input on same line python

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

How can I make it that when the user enters input its on the same line as the print statement

like for example for the code snippet below. The output becomes:

Enter grade for course 1: A
Enter credits for course 1: 4

For now this is what I get:

Enter grade for course 1: 
A
Enter credits for course 1:
4

Here is the code snippet

for i in range(1,coursenumber+1):
    print("Enter grade for course ", i,":", end =""),
    grade=str(input())
    print("Enter credits for course", i,":", end =" ")
    credit=int(input())
    totalgpa+=translate(credit,grade)
    totalcredit+=credit

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: 
     

    Input on same line 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. 

    Input on same line 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 : 
     

    Input on same line 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 take inputs on the same line?

    b) input () You can take multiple inputs in one single line by using the raw_input function several times as shown below.

    How do you enter multiple values on the same line in Python?

    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 : This function helps in getting multiple inputs from users. It breaks the given input by the specified separator.

    How do you take multiple integer inputs in a single line in Python?

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