Is there any straightforward way in Python to strip a string and get the start index and the end index?
Example: Given the string ' hello world! '
, I want to the stripped string 'hello world!'
As well as the start index 2
and the and index 14
.
' hello world! '.strip()
only returns the stripped string.
I could write a function:
def strip(str):
'''
Take a string as input.
Return the stripped string as well as the start index and end index.
Example: ' hello world! ' --> ('hello world!', 2, 14)
The function isn't computationally efficient as it does more than one pass on the string.
'''
str_stripped = str.strip()
index_start = str.find(str_stripped)
index_end = index_start + len(str_stripped)
return str_stripped, index_start, index_end
def main():
str = ' hello world! '
str_stripped, index_start, index_end = strip(str)
print('index_start: {0}\tindex_end: {1}'.format(index_start, index_end))
if __name__ == "__main__":
main()
but I wonder whether Python or one popular library provides any built-in way to do so.