I have the string x = "123+456-789"
and I want to have the "123+456-"
and "789"
in separate variables. How can I store the last few characters of a string excluding the character "-"
in a variable and store the rest in a different variable? This also has to work no matter how many "+"
or "-"
or other arithmetic operators are in the string (the last few digits excluding special characters have to be in a separate variable).
I have the last characters but I need the first few to be stored in a different variable:
>>> equation_text = "123+456-789"
>>> re.split(r"[+-/*%]", equation_text)[-1]
'789'
I used slicing:
>>> re.split(r"[+-/*%]", equation_text)[:-1]
['123', '456']
But the "+"
and "-"
are missing.