How to take input in python separated by space

Use input[], map[] and split[] function to take space-separated integer input in Python 3. You have to use list[] to convert the map to a list.

list[map[int,input[].split[]]] 

Where:

  • 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

Simple example code stage user multiple integers input, each separated space.

print["Enter the numbers: "]

inp = list[map[int, input[].split[]]]

print[inp]

Output:

Do comment if you have any doubts and suggestions on this Python input program.

Note: IDE: PyCharm 2021.3 [Community Edition]

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Like this:

In [20]: a,b = raw_input[].split[]
12 12.2

In [21]: a = int[a]
Out[21]: 12

In [22]: b = float[b]
Out[22]: 12.2

You can't do this in a one-liner [or at least not without some super duper extra hackz0r skills -- or semicolons], but python is not made for one-liners.

answered Nov 12, 2010 at 8:35

Gabi PurcaruGabi Purcaru

30.1k9 gold badges76 silver badges91 bronze badges

One liner :]

>>> [f[i] for f,i in zip[[int, float], raw_input[].split[]]]
1 1.2
[1, 1.2]

answered Nov 12, 2010 at 8:44

1

Simpler one liner[but less secure]:

map[eval, raw_input[].split[]]

answered Sep 2, 2013 at 20:27

0

If the input is separated by spaces " "

a,b,c = raw_input[].split[" "]

If the input is separated by comma ','

a,b,c = raw_input[].split[","]

alex

5,2394 gold badges33 silver badges43 bronze badges

answered Aug 6, 2016 at 5:56

In Python 2.7, I use this

A,B = raw_input[].split[" "]
A = int[A]
B = float[B]
print[A]
print[B]

Output:

34 6.9

34

6.9

answered Apr 3, 2017 at 8:20

JasmohanJasmohan

2273 silver badges5 bronze badges

map[str,input[].split[]] that is how you do it.

ρss

4,9578 gold badges42 silver badges71 bronze badges

answered Jan 31, 2016 at 17:45

0

Below snippet works for me.

>>> a,b=list[map[int,input[].split[]]]
1 2
>>> print[a]
1
>>> print[b]
2

answered Dec 16, 2020 at 4:24

If you wish to take as many inputs as u want then following:

    x=list[map[str,input[].split[]]] 
    print[x]

If you want two inputs:

   x,y=x,y=list[map[str,input[].split[]]]
   print[x,y]

answered Jul 11, 2019 at 19:10

semssems

212 bronze badges

1

This is good solution imho a, b = input[].split[].

If you want to separate input with custom character you can put it in parenthesis e.g. a, b = input[].split[","]

answered Sep 16, 2019 at 2:44

Read 3 inputs separated by space...

arr = input[].split[" "]
A = float[arr[0]]
B = float[arr[1]]
C = float[arr[2]]
print[A]
print[B]
print[C]

answered Dec 2, 2019 at 13:45

Python 3.5

Below snippet works for me.

a, b = input[].split[" "]
a_value = int[a]
b_value = int[b]

answered Dec 15, 2019 at 10:56

vijayraj34vijayraj34

1,62822 silver badges24 bronze badges

Basically you just need to use map function and pass user define function for which datatype you need to convert.

code

You can even use it to convert it into other datatypes as well

answered Jan 12, 2021 at 5:25

1

In python 3 we can use,

r1,r2 = map[int,input[].split[" "]]

answered Jan 23 at 5:31

1

Not the answer you're looking for? Browse other questions tagged python or ask your own question.

How do you take space separated inputs?

There are 2 methods to take input from the user which are separated by space which are as follows:.
Using BufferedReader Class and then splitting and parsing each value..
Using nextInt[ ] method of Scanner class..

How do you print a string separated by space in Python?

To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.

Chủ Đề