andrew andrew - 1 year ago 93
Linux Question

How to exclude substring from string closing with whitespace?

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


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

Answer Source

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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download