andrew andrew - 1 month ago 5
Linux Question

How to exclude substring from string closing with whitespace?

I would like to exclude substring starting from '>' and closing with ' '.
Example:

#!/usr/bin/python

content = "classTest>528 points"
substring = content['>' : ' ']
print substring # 528

Answer

If you do not want to use regular expressions (which has a bit of a learning curve), you can use this simpler but longer code.

content = "classTest>528 points"
i = content.find('>')
j = content.find(' ', i+1)
substring = content[i+1:j]
print(substring)
Comments