Python | Extract substrings between brackets
Sometimes, while working with Python strings, we can have a problem in which we need to extract the substrings between certain characters and can be brackets. This can have application in cases we have tuples embedded in string. Lets discuss certain ways in which this task can be performed.
Method #1 : Using regex
One way to solve this problem is by using regex. In this we employ suitable regex and perform the task of extraction of required elements.
The original string is : geeks(for)geeks is (best) The element between brackets : ['(for)', '(best)']
Method #2 : Using list comprehension +
isintance() + eval()
The combination of above methods can also be used to solve this problem. In this eval() assume the brackets to be tuples and helps the extraction of strings within them.
The original string is : [(234, ), 4, (432, )] The element between brackets : ['(234, )', '(432, )']
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course