Just split the string on whitespace, and get the last element of the array. Or use rsplit()
to start splitting from end:
>>> st = 'Hello my name is John'
>>> st.rsplit(' ', 1)
['Hello my name is', 'John']
>>>
>>> st.rsplit(' ', 1)[1]
'John'
The 2nd argument specifies the number of split
to do. Since you just want last element, we just need to split once.
As specified in comments, you can just pass None
as 1st argument, in which case the default delimiter which is whitespace will be used:
>>> st.rsplit(None, 1)[-1]
'John'
Using -1
as index is safe, in case there is no whitespace in your string.