How to store everything before and after a special character?

"t1 = 2; a[0] = 1; a[1] = 2; a[t1] = 3;"

In the above string I want to store all variables and constants separately like:

var = ['t1', 'a[0]', 'a[1]', 'a[t1]']

constants = ['2', '1', '2', '3']

Answer

Using Regex.

Ex:

import re

s = "t1 = 2; a[0] = 1; a[1] = 2; a[t1] = 3;"
var, constants = [], []
for v, c in re.findall(r"(.+?) = (.+?);", s):
    var.append(v)
    constants.append(c)
print(var)
print(constants)

Output:

['t1', ' a[0]', ' a[1]', ' a[t1]']
['2', '1', '2', '3']