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
   ^
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
Subha Jeet Sikdar
  • 446
  • 1
  • 4
  • 15

4 Answers4

2

Use raw_input:

inp = raw_input()

x = inp.split()

print x[0]
print x[1]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
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()))) )
raunak rathi
  • 95
  • 1
  • 9
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.

Community
  • 1
  • 1
Olamigoke Philip
  • 1,035
  • 8
  • 10
0

Taking two inputs at a time:

a, b = input("Enter a two value: ").split()
Nick
  • 4,820
  • 18
  • 31
  • 47