Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Space separated input in python

Tags:

python

input

Here is the input specification:

The program has to read t lines of inputs. Each line consist of 2 space separated values first one is the name and second is the age.

An Example of input:

Mike 18
Kevin 35
Angel 56

How to read this kind of input in Python? If I use raw_input(), both name and age are read in the same variable.

like image 829
gibraltar Avatar asked Oct 03 '11 16:10

gibraltar


People also ask

How do you accept a space separated input in Python?

Use input(), map() and split() function to take space-separated integer input in Python 3.

How do you read n space separated integers in Python?

Thanks. Easy solution is just to use the string method split. input: 5 8 0 sgshsu 123 input. split(" ") == ["5", "8", "0", "sgshsu", "123"] #Then they are easy to convert to numeric datatypes, but invalid inputs might raise errors.

How do you read a space in Python?

Python String isspace() method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as: ' ' – Space. '\t' – Horizontal tab.


1 Answers

the_string = input()
name, age = the_string.split()

In Python 2, use raw_input instead of input.

like image 70
andreaspelme Avatar answered Oct 02 '22 15:10

andreaspelme