0

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.

Nat Kar
  • 31
  • 7
  • 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:58

1 Answers1

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