How do you take two inputs separated by space in python?

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.2k9 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,2694 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

How do you take two inputs separated by space in python?

answered Apr 3, 2017 at 8:20

JasmohanJasmohan

2273 silver badges5 bronze badges

map(str,input().split()) that is how you do it.

How do you take two inputs separated by space in python?

ρss

4,9778 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

How do you take two inputs separated by space in python?

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

How do you take two inputs separated by space in python?

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

How do you take two inputs separated by space in python?

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

How do you take two inputs separated by space in python?

vijayraj34vijayraj34

1,65822 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

How do you take two inputs separated by space in python?

1

In python 3 we can use,

r1,r2 = map(int,input().split(" "))

answered Jan 23 at 5:31

How do you take two inputs separated by space in python?

1

How do you read two 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 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 take space separated input in for loop in Python?

how to get user input of list of lists in python.
lst = [ ].
n = int(input("Enter number of elements : ")).
for i in range(0, n):.
ele = [input(), int(input())].
lst. append(ele).
print(lst).

How do you read a string with spaces 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.