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.
Use input(), map() and split() function to take space-separated integer input in Python 3.
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.
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.
the_string = input()
name, age = the_string.split()
In Python 2, use raw_input
instead of input
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With