How to extract all numbers from string in Python

2 Answers

0 votes
import re
  
s = 'a213python 34 java98 c++ 88397 php 3.14 swift 6-89x'
  
lst = re.findall('[0-9]+', s)
  
print(lst)
    
    
    
'''
run:
    
['213', '34', '98', '88397', '3', '14', '6', '89']
  
'''

 



answered Dec 16, 2020 by avibootz
edited Dec 17, 2020 by avibootz
0 votes
s = 'a213python 34 java98 c++ 88397 php 3.14 swift 6-89x'
   
lst = [int(temp)for temp in s.split() if temp.isdigit()]
   
print(lst)
     
     
     
'''
run:
     
[34, 88397]
   
'''

 



answered Apr 12, 2021 by avibootz

Related questions

1 answer 160 views
1 answer 164 views
3 answers 379 views
3 answers 256 views
1 answer 203 views
...