In Python how do you split a string into variables when the number of variables depends on the input string. Ex.) I want to split " (1,2,3) (2,3,4) …" into two variables (a=123 b=234)... but if more groups were inputted more variables would be created.
Asked
Active
Viewed 1,518 times
1 Answers
0
Simply do this:
>>> s="abc def"
>>> a,b=s.split()
>>> a
'abc'
>>> b
'def'
>>>
Update:
from string import ascii_letters as letters
s="1 2 3 4 5 6 7"
for i,v in enumerate(s.split()):
globals()[letters[i]]=v
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
Output:
1
2
3
4
5
6
7

U13-Forward
- 69,221
- 14
- 89
- 114
-
This requires knowing how many variables you're going to split the string into in advance. Definitely not a variable number of variables. – blhsing Aug 08 '18 at 01:52
-
-
Could someone elaborate on how to create variables when you don't know exactly how many will be made? – Nat Kar Aug 15 '18 at 00:57
-