Don't know if it is "the best way" but you could do this with regex and it will automatically pick the last digit in a string:
EDIT
This pattern searches for any digit (or digits) which isn't followed by another digit in the string. So you always get the last number as a result.
With re.search:
lst = ["im.png", "Image 02.tif", "My3rdImage_3.jpg" , "Whatever_17_MoreWhatever-31.tiff", "My4rdImage22_445.jpg"]
pat = r"(\d+)(?!.*\d+)"
for elem in lst:
tmp = re.search(pat, elem)
if tmp:
print(tmp[0])
02
3
31
445
Or (credit to @Mad Physicist) a pattern which searches for "A single group of digits followed by non-digits, then end-of-string"
lst = ["im.png", "Image 02.tif", "My3rdImage_3.jpg" , "Whatever_17_MoreWhatever-31.tiff", "My4rdImage22_445.jpg"]
pat = r"(\d+)(?=\D*$)"
for elem in lst:
tmp = re.search(pat, elem)
if tmp:
print(tmp[0])
02
3
31
445
re.findall
import re
lst = ["im1.png", "Image 02.tif", "My3rdImage_3.jpg" , "Whatever_17_MoreWhatever-31.tiff"]
pattern = r"\d+"
for elem in lst:
tmp = re.findall(pattern, elem)
print(tmp)
print(tmp[-1], '\n')
['1']
1
['02']
02
['3', '3']
3
['17', '31']
31