22

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.

1
  • 3
    First grab some Python tutorial. Then learn how to use split Commented Oct 3, 2011 at 16:40

9 Answers 9

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

In Python 2, use raw_input instead of input.

Sign up to request clarification or add additional context in comments.

2 Comments

what if i have to read multiple integers like thousands or millions, and operate on them on the time of input.
raw_input isn't use in python 3 any more, you should uselist(map(str,input().split()))
8

If you have it in a string, you can use .split() to separate them.

>>> for string in ('Mike 18', 'Kevin 35', 'Angel 56'):
...   l = string.split()
...   print repr(l[0]), repr(int(l[1]))
...
'Mike' 18
'Kevin' 35
'Angel' 56
>>>

Comments

6

Assuming you are on Python 3, you can use this syntax

inputs = list(map(str,input().split()))

if you want to access individual element you can do it like that

m, n = map(str,input().split())

1 Comment

that won't work because first item is not an integer
3

You can do the following if you already know the number of fields of the input:

client_name = raw_input("Enter you first and last name: ")
first_name, last_name = client_name.split() 

and in case you want to iterate through the fields separated by spaces, you can do the following:

some_input = raw_input() # This input is the value separated by spaces
for field in some_input.split():
    print field # this print can be replaced with any operation you'd like
    #             to perform on the fields.

A more generic use of the "split()" function would be:

    result_list = some_string.split(DELIMITER)

where DELIMETER is replaced with the delimiter you'd like to use as your separator, with single quotes surrounding it.

An example would be:

    result_string = some_string.split('!')    

The code above takes a string and separates the fields using the '!' character as a delimiter.

1 Comment

How to separate input as individual characters without any whitespace in between. For example, I want the input ABC to be read in the matrix as 'A', 'B', 'C'. I am aware that I can use list("ABC") to separate but I want to use split() instead.
2

For python 3 it would be like this n,m,p=map(int,input().split())

Comments

1

For python 3 use this

inp = list(map(int,input().split()))
#input => java is a programming language
#return as => ("java","is","a","programming","language")

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 object

Finally list() converts the map to a list

Comments

1

Space separated values inside an array with specific range:

arr = list(map(int, input().split()))[:n]

here n is denoting the maximum length of list.

Comments

0
a=[]
for j in range(3):
    a.append([int(i) for  i in 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']].

Comments

-2

For Python3:

a, b = list(map(str, input().split()))
v = int(b)

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.