How to take input in python with space

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 to take input in python with space

answered Apr 3, 2017 at 8:20

JasmohanJasmohan

2273 silver badges5 bronze badges

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

How to take input in python with space

ρ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 to take input in python with space

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 to take input in python with space

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 to take input in python with space

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 to take input in python with space

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 to take input in python with space

1

In python 3 we can use,

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

answered Jan 23 at 5:31

How to take input in python with space

1

How do you input a single line with space?

In python..
Single line input array without space between them..
N=[int(i) for i in input().split(“”)].
Single line input array with space between them..
N=[int(i) for i in input().split(“ ”)].
Thanx for reading..
If u want any modification to be done plz let me know..

How do you take a list of inputs separated by space in Python?

Use an input() function to accept the list elements from a user in the format of a string separated by space. Next, use a split() function to split an input string by space. The split() method splits a string into a list.

How do you accept N space separated integers in Python?

2 Answers.
using input() 1 and. This prompts the user to enter inputs: int_list = [int(x) for x in input("Enter integers:").split()] . split() separates the values in the input by spaces..
using sys.argv , you can specify input from the command line import sys int_list = [int(x) for x in sys.argv[1:]].

How do you print numbers with spaces in Python?

To give multiple spaces between two values, we can assign space in a variable, and using the variable by multiplying the value with the required spaces. For example, there is a variable space assigned with space and we need to print 5 spaces - we can use space*5 and it will print the 5 spaces.

How do you input without space in Python?

strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.