Get everything after python character last appeared

I saw this in other languages, but I can not find it for Python, maybe it has a specific name that I do not know about. I want to split the line after the last occurrence of the '-' character, my line will look like this:

POS--K    100    100    001    -    1462

I want to take the last number from this line after the last - in this case 1462. I have no idea how to achieve this in order to achieve this when only one such character is expected, I would use the following:

last_number = last_number[last_number.index('-')+1:]

How could I achieve this when an unknown number can be present, and the final number can be of any length?

+4
source share
2 answers
. index , rindex :
>>> s = 'POS--K    100    100    001    -    1462'
>>> s[s.rindex('-')+1:]
'    1462'
+13

, -

>>> s = "POS--K    100    100    001    -    1462"
>>> a = s.split('-')[-1]
>>> a
'    1462'
>>> a.strip()
'1462'

Padraic, comment, rsplit

>>> s = "POS--K    100    100    001    -    1462"
>>> a = s.rsplit('-')[1]
>>> a
'    1462'
>>> a.strip()
'1462'
+4

All Articles