Nitika
Nitika

Reputation: 215

How to take space separated input in python

I want to take the following in single line from user

Abc 0 1 0

How can this be done? I tried

map(int,input().split())
input() and then split
input().split(" ")

I am getting the following error:

  File "main.py", line 11, in <module>
    name = input()
  File "<string>", line 1
    Abc 0 0 2
        ^
SyntaxError: invalid syntax

but it is not giving the desired result.

Upvotes: 0

Views: 2930

Answers (3)

Gulzar
Gulzar

Reputation: 28074

#a = input()
a = "Abc 0 1 0"
inp_list = a.split()
print(inp_list)

input() returns a list of strings. You have to handle it as such

for e in inp_list:
    try:
        e_int = int(e)
    except ValueError:
        print(e)
        continue
    print(e_int)

Upvotes: 1

Brett La Pierre
Brett La Pierre

Reputation: 533

text = input("Say something")
print(text.split())
print(text.replace(" ", ""))

output

Say somethingthis has spaces but it wont when it is done
['this', 'has', 'spaces', 'but', 'it', 'wont', 'when', 'it', 'is', 'done']
thishasspacesbutitwontwhenitisdone

Upvotes: 1

TUNAPRO1234
TUNAPRO1234

Reputation: 106

Try this:

inp1, inp2, inp3, inp4 = input().split(“ “)

You could directly unpack them in a function like that:

func(*input().split(“ “))

Upvotes: 1

Related Questions