Skip to content Skip to sidebar Skip to footer

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

Solution 1:

Try this:

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

You could directly unpack them in a function like that:

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

Solution 2:

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

Solution 3:

#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)
        continueprint(e_int)

Post a Comment for "How To Take Space Separated Input In Python"