0

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.

2
  • Have you tried "splitting on space" when you have space-separated input? Commented Oct 24, 2020 at 7:22
  • I want to take the input and then split Commented Oct 24, 2020 at 7:23

3 Answers 3

1

Try this:

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

You could directly unpack them in a function like that:

func(*input().split(“ “))
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting File "main.py", line 11, in <module> name = input().split(" ") File "<string>", line 1 Abc 0 0 2 ^ SyntaxError: invalid syntax
Use # to comment out the first line of code you posted
1
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

1 Comment

I want to take input from user
1
#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)

5 Comments

I am getting File "main.py", line 11, in <module> name = input() File "<string>", line 1 Abc 0 0 2 ^ SyntaxError: invalid syntax
@Nitika You are getting this error on your own code...
even when I writing this single line I am getting the same error.
@Nitika When running my code, you will open a console. In that newly opened console, you have to type your input. Not in the code itself.
I am doing the same. Can you provide a code snippet with just the input line and then give the input in console?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.