0

How to get multiple inputs in one line in Python?

Here's what I want:

Python Input:

Enter name and age respectively: Subha 18

Output:

Your Name Is Subha and Your age is 18 years

Here is what I tried:

inp = input()

x = inp.split()

print x[0]   # Won't work
print x[1]

The error is (console):

SyntaxError: unexpected EOF while parsing
Traceback (most recent call last):
File "source_file.py", line 3, in <module>
inp = input()
File "<string>", line 1
11 22
   ^
3
  • Related question: raw_input function in Python Commented Jul 28, 2019 at 7:16
  • 3
    time to switch to Python 3 before support for 2 ends? Commented Jul 28, 2019 at 7:18
  • @Aprillion i will think about it Commented Jul 28, 2019 at 7:23

4 Answers 4

2

Use raw_input:

inp = raw_input()

x = inp.split()

print x[0]
print x[1]
Sign up to request clarification or add additional context in comments.

1 Comment

but i think raw_input is for python 2?
1

Python 3

There are multiple ways to get input in one line python statement, but that totally depends on data type choice.

For storing data in variable

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

e.g.;

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

For storing data in the list.

list_name = list(map(data_type,intput().split()) 

or

list_name = [data_type(input()) for i in range(n)]

for n could be range of input.

For storing input in the dictionary.

dict1= dict(zip(range(n),list(map(int,input().split()))) )

Comments

0

Python 3

>>> init_input = input("Enter name and age respectively, e.g 'Shubha 18': ")

Enter name and age respectively, e.g 'Shubha 18': Marcus 21

>>> values = init_input.split(" ") Returns a list: ['Marcus', '21']

The split() method returns a list of all the words in the string, taking the separator as an argument.

Comments

0

Taking two inputs at a time:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.