How to get the last N numbers from a string in Python

1 Answer

0 votes
import re
  
s = 'python 34 java98 c++ 88397 php 3.14 swift 6-89'
  
lst = re.findall('[0-9]+', s)

print(lst)  
  
n = 3

print(lst[len(lst) - n : len(lst)])

   
    
'''
run:
    
['34', '98', '88397', '3', '14', '6', '89']
['14', '6', '89']
 
'''

 



answered Oct 27, 2020 by avibootz

Related questions

...