21

I want to read two input values. First value should be an integer and the second value should be a float.

I saw Read two variables in a single line with Python, but it applies only if both the values are of same type. Do I have any other way?

Example input, first is int and second is float. The inputs should be on a single line:

20 150.50

http://www.codechef.com/problems/HS08TEST/

I'm very new to Python.

Community
  • 1
  • 1
Prince Ashitaka
  • 8,623
  • 12
  • 48
  • 71
  • 2
    The answer to the linked question is most of the answer you need. What have you tried? – SingleNegationElimination Nov 12 '10 at 08:35
  • @TokenMacGuy mistake is mine. I should have read doc's first :( – Prince Ashitaka Nov 12 '10 at 09:01
  • Please follow [general](http://tinyurl.com/so-hints) question [guidelines](http://meta.stackexchange.com/q/10812): state any special restrictions, show what you've tried so far, and ask about what specifically is confusing you. –  Nov 12 '10 at 13:58

13 Answers13

31

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.

Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
4

Simpler one liner(but less secure):

map(eval, raw_input().split())
Phoris
  • 41
  • 1
4

One liner :)

>>> [f(i) for f,i in zip((int, float), raw_input().split())]
1 1.2
[1, 1.2]
dietbuddha
  • 8,556
  • 1
  • 30
  • 34
3

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,467
  • 4
  • 33
  • 43
3

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
Jasmohan
  • 237
  • 3
  • 5
2

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

ρss
  • 5,115
  • 8
  • 43
  • 73
1

Below snippet works for me.

>>> a,b=list(map(int,input().split()))
1 2
>>> print(a)
1
>>> print(b)
2
Shaila B
  • 11
  • 2
0

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)
sems
  • 21
  • 2
  • The result of `x=input().split()` is a list of strings so you are performing `map` and `list` unnecessarily. Oddly, you also have `x,y=x,y=` in your second example. – David Buck Dec 02 '19 at 14:20
0

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(",")

0

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)
0

Python 3.5

Below snippet works for me.

a, b = input().split(" ")
a_value = int(a)
b_value = int(b)
vijayraj34
  • 2,135
  • 26
  • 27
-1

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

Manan
  • 1
-1

In python 3 we can use,

r1,r2 = map(int,input().split(" "))
  • This way both numbers will be `int` while question specifies that second is float. – MjH Jan 23 '22 at 09:42