Python: Extract text between brackets

Extract text between two square brackets in python.
Example string: Get[How]Stuff
Expected Output: How

Below is the python script

string="Get[How]Stuff"
#position of opening square bracket
firstpos=string.rfind("[")
#position of closing square bracket
lastpos=string.rfind("]")
#printing the text between two square brackets
print(string[firstpos+1:lastpos])#Prints output 'How'

About the Author

SRINI S

A passionate blogger. Love to share solutions and best practices on wordpress hosting, issues and fixes, excel VBA macros and other apps

Leave a Reply

Your email address will not be published. Required fields are marked *