Let's suppose I have this string:
s = "123(45)678"
How can I can get this list?
l = ['123','(','45',')','678']
If you were only interested in '('
or ')'
then str.partition
would have been sufficient.
Since you have multiple delimiters AND you want to keep them, you can use re.split
with a capture group:
import re
s = "123(45)678"
print(re.split(r'([()])', s))
# ['123', '(', '45', ')', '678']
You can use re.findall
:
import re
s = "123(45)678"
final_data = re.findall('\d+|\(|\)', s)
print(final_data)
Output:
['123', '(', '45', ')', '678']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With