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.
Update I am going to respecify the question. We already know how to read formatted input in python. Is there a way we can read formatted input in Python or not? If yes then how?
For python 3 use this
input() accepts a string from STDIN.
split()
splits the string about whitespace character and returns a list of strings.map()
passes each element of the 2nd argument to the first argument and returns a map objectFinally
list()
converts the map to a listIf you have it in a string, you can use
.split()
to separate them.For python 3 it would be like this
n,m,p=map(int,input().split())
In this above code the given input i.e Mike 18 Kevin 35 Angel 56, will be stored in an array 'a' and gives the output as [['Mike', '18'], ['Kevin', '35'], ['Angel', '56']].
You can do the following if you already know the number of fields of the input:
and in case you want to iterate through the fields separated by spaces, you can do the following:
A more generic use of the "split()" function would be:
where DELIMETER is replaced with the delimiter you'd like to use as your separator, with single quotes surrounding it.
An example would be:
The code above takes a string and separates the fields using the '!' character as a delimiter.
Assuming you are on Python 3, you can use this syntax
if you want to access individual element you can do it like that