Method # 1: Using re.split()
This task can be accomplished using the split function provided by the Python regex library that can split string under certain conditions, and in this case — all numbers and operators.
|
Output:
The original string is: 15 + 22 * 3-4 / 2
The list after performing split functionality: [’15’, ’+’, ’22’, ’*’, ’3’, ’-’, ’4’, ’/’, ’2’]
Method # 2: Using re.findall ()
This Python regex library function can also do the same task as the above function. It can also support decimal numbers as additional functionality.
|
Output:
The original string is: 15 + 22.6 * 3-4 / 2
The list after performing split functionality: [’15’, ’+’, ’22 .6 ’,’ * ’ , ’3’, ’-’, ’4’, ’/’, ’2’]Python | Splitting operators in a string Python functions: Questions
Python | Splitting operators in a string split: Questions