I want to assign values to variable using .split() | Sololearn: Learn to code for FREE!

0

I want to assign values to variable using .split()

I want to pass values in a single line with spaces ex:- 2 3 1 1 1. 2 should be assigned to A , 3 should be assigned to B and the remaining numbers should be assigned to a list .How can I do it? I tried A,B,C = map(int, input ().split()) but it shows error

1/23/2022 12:51:03 PM

syed fahad

8 Answers

New Answer

+4

e.g.: a, b, *c = input().split() Note the *

+4

You were almost there: A, B, *C = map(int, input().split())

+2

Try A, B, *C = map(..blah blah...), If not got my ide up and running at the moment and it's been a while since I've done any python.

+2

If only the last values are int, you could try to only convert the non-string variables: a, b, *c = input().split() a, b = int(a), int(b)

+1

you have to prefix C with * like *C so that it can assign the rest of the numbers, as it is variable. So: A,B,*C = map(int, input().split()) without it it would throw an error because the rest has not been assigned

0

Thanks Everyone

0

one more question what if the last values are string type?

0

n = int(Input ()) ls = list(n.split()) A = ls[0] B = ls[1] simplest one, don't go for mapped ,zip functions it will only drain your brain's battery