Extract string between two delimiters

Simple python script to extract sub string between two delimiters of a string. Examples of delimiters are brackets, parentheses, braces, single or double quotes etc. You can also customize this code to extract the string between two special characters or two sub strings. Comments on each line of code explains you to easily understand the code.

Note: This code is written in Python 2

string = "sri[nivas]silveri"
firstDelPos=string.find("[") # get the position of delimiter [
secondDelPos=string.find("]") # get the position of delimiter ]
extractedString = string[firstDelPos+1:secondDelPos] # get the string between two dels
print extractedString # print the extracted string between two dels

Output:

python-script-extract-string-output-on-windows-powershell

Python: Replace/remove string between two delimiters/characters

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 *