Python substring extraction is primarily done using string slicing, which uses the syntax string[start:end:step]. This allows you to extract a portion of a string based on start and end indices (with the end index being exclusive) and an optional step value.
Get substring from start index to end:
string[2:7]returns characters from index 2 up to (but not including) index 7.Get substring from start to end of string: Omit the end index:
string[2:]returns from index 2 to the end.Get substring from beginning to end index: Omit the start index:
string[:5]returns the first 5 characters.Get last n characters: Use negative indexing:
string[-3:]returns the last 3 characters.Get a single character: Use a single index:
string[0]returns the first character;string[-1]returns the last.
Key points:
Python uses 0-based indexing.
If the end index exceeds the string length, Python returns the available portion without raising an error.
Negative indices count from the end of the string.
Use
into check if a substring exists:'test' in 'testing'returnsTrue.Use
.find()to get the index of a substring (returns -1 if not found), or.index()which raises an exception if not found.
For more complex patterns, use the re module with re.search() and .group() to extract substrings based on regular expressions.
Copy>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'
Python calls this concept "slicing" and it works on more than just strings. Take a look here for a comprehensive introduction.
Answer from Paolo Bergantino on Stack OverflowCopy>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'
Python calls this concept "slicing" and it works on more than just strings. Take a look here for a comprehensive introduction.
Just for completeness as nobody else has mentioned it. The third parameter to an array slice is a step. So reversing a string is as simple as:
Copysome_string[::-1]
Or selecting alternate characters would be:
Copy"H-e-l-l-o- -W-o-r-l-d"[::2] # outputs "Hello World"
The ability to step forwards and backwards through the string maintains consistency with being able to array slice from the start or end.
How to extract a substring from a string in Python 3 - Stack Overflow
Python: best way to find and extract substring from a string?
How to find all occurrences of a substring in a string while ignore some characters in Python?
Medium Leetcode question - Longest Substring Without Repeating Characters
Videos
Many many ways to solve this. Here are two examples:
First one is a simple replacement of your unwanted characters.
targetstring = '[<THIS STRING-STRING-STRING THAT THESE THOSE>]'
#ALTERNATIVE 1
newstring = targetstring.replace(r" THAT THESE THOSE>]", '').replace(r"[<THIS ", '')
print(newstring)
and this drops everything except your target pattern:
#ALTERNATIVE 2
match = "STRING-STRING-STRING"
start = targetstring.find(match)
stop = len(match)
targetstring[start:start+stop]
These can be shortened but thought it might be useful for OP to have them written out.
I found this extremely useful, might be of help to you as well: https://www.computerhope.com/issues/ch001721.htm
If by '"[<THIS " &" THAT THESE THOSE>]" are static' you mean that they are always the exact same string, then:
s = "[<THIS STRING-STRING-STRING THAT THESE THOSE>]"
before = len("[<THIS ")
after = len(" THAT THESE THOSE>]")
s[before:-after]
# 'STRING-STRING-STRING'