Sometimes we have a source string to have certain mathematical statement for computations and we need to split both the numbers and operators as a list of individual elements. Let’s discuss certain ways in which this problem can be performed.
Method #1 : Using re.split()
This task can be solved using the split functionality provided by Python regex library which has power to split the string according to certain conditions and in this case all numbers and operators.
# Python3 code to demonstrate working of # Splitting operators in String # Using re.split() import re # initializing string test_str = "15 + 22 * 3-4 / 2" # printing original string print("The original string is : " + str(test_str)) # Using re.split() # Splitting operators in String res = re.split(r'(\D)', test_str) # printing result print("The list after performing split functionality : " + str(res)) |
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 also can perform the similar task as the above function. It can also support decimal numbers as additional functionality.
# Python3 code to demonstrate working of # Splitting operators in String # Using re.findall() import re # initializing string test_str = "15 + 22.6 * 3-4 / 2" # printing original string print("The original string is : " + str(test_str)) # Using re.findall() # Splitting operators in String res = re.findall(r'[0-9\.]+|[^0-9\.]+', test_str) # printing result print("The list after performing split functionality : " + str(res)) |
The original string is : 15+22.6*3-4/2
The list after performing split functionality : [’15’, ‘+’, ‘22.6’, ‘*’, ‘3’, ‘-‘, ‘4’, ‘/’, ‘2’]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Splitting string to list of characters
- Python | Splitting string to list of characters
- Python | Splitting list on empty string
- Python | Splitting Text and Number in string
- Python | Splitting string list by strings
- NLP | Splitting and Merging Chunks
- Splitting Arrays in NumPy
- Logical Operators on String in Python
- Increment and Decrement Operators in Python
- Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
- Inplace Operators in Python | Set 2 (ixor(), iand(), ipow(),…)
- Inplace vs Standard Operators in Python
- Chaining comparison operators in Python
- Python Membership and Identity Operators | in, not in, is, is not
- Python Operators
- Python | Solve given list containing numbers and arithmetic operators
- Python Logical Operators with Examples
- Python Bitwise Operators
- Merging and Updating Dictionary Operators in Python 3.9
- Python 3 - Logical Operators
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

